LoginSignup
34
40

More than 5 years have passed since last update.

Rayを飛ばして当たったオブジェクトの情報を得る方法

Last updated at Posted at 2016-11-20
void RayTest()
    {
        //Rayの作成       ↓Rayを飛ばす原点   ↓Rayを飛ばす方向
        Ray ray = new Ray (transform.position, new Vector3 (0, -1, 0));

        //Rayが当たったオブジェクトの情報を入れる箱
        RaycastHit hit;

        //Rayの飛ばせる距離
        int distance = 10;

        //Rayの可視化    ↓Rayの原点    ↓Rayの方向         ↓Rayの色
        Debug.DrawLine (ray.origin, ray.direction * distance, Color.red);

        //もしRayにオブジェクトが衝突したら
        //                  ↓Ray  ↓Rayが当たったオブジェクト ↓距離
        if (Physics.Raycast(ray,out hit,distance))
        {
              //Rayが当たったオブジェクトのtagがPlayerだったら
                if (hit.collider.tag == "Player")
                Debug.Log ("RayがPlayerに当たった");
        }
    }
34
40
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
34
40