unity基础开发----射线碰撞代码

C# 射线碰撞代码  
        if(Input.GetMouseButtonDown(0))
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
           
            if (Physics.Raycast(ray,out hit))
            {
                Debug.DrawLine(ray.origin, hit.point);
                //print(hit.collider.gameObject.name);

                //curObject = hit.collider.gameObject;

                // 显示当前选中对象的名称
                print(hit.collider.gameObject);
             }

        }

touch版

  foreach (Touch touch in Input.touches)
        {
            if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved)
            {
                Ray ray = Camera.main.ScreenPointToRay(new Vector3(touch.position.x,touch.position.y,0));
                RaycastHit hit;

                if (Physics.Raycast(ray, out hit))
                {
                    Debug.DrawLine(ray.origin, hit.point);

                    // 显示当前选中对象的名称   
                    print(hit.collider.gameObject);
                    if (hit.collider.gameObject.name == "Cube")
                    {
                       
                    }
                 

        }


你可能感兴趣的:(unity基础开发----射线碰撞代码)