Untiy射线获取对象

using UnityEngine;
using System.Collections;

public class HeroControl : MonoBehaviour {
public Ray ray1;
public Camera ca;
public float roate_Speed = 1F;
public string[] cubeAll=new string[11];
public GameObject Sphere1; //摄像机环绕的对象
public Transform target_transform = null; //射线点击到的物体平面
public float k = 2; //环绕速度系数
public int i;
public int j;
private int Number = 11;
public string a=”Cube”;
public string b;
// Use this for initialization
void Start () {
NumberForCube();
}

// Update is called once per frame
void Update () {
    Camerarotate();
    DestoryCube();

}
void NumberForCube()
{
    for (i = 0; i < Number; i++)
    {
        //for (j = 0; j < Number; j++)
        //{
            cubeAll[i] = a + i.ToString();
        //}


        print(cubeAll[i]);
    }
}
void Camerarotate()
{
    if (Input.GetMouseButton(0))
    {


        Ray rayObj = Camera.main.ScreenPointToRay(Input.mousePosition);     //在屏幕上转换坐标:将鼠标点转换成射线  
        RaycastHit hitObj;
        if (Physics.Raycast(rayObj, out hitObj))
        {
            //Debug.Log("射线得到的对象名称:" + hitObj.collider.name);  
            //target_transform = hitObj.transform;
        }
        if (target_transform != null)   //如果射线投射到物体上(射线射到物体上得到反射)
        {
            //Debug.Log("射线取得对象");  
            float mousX = Input.GetAxis("Mouse X") * roate_Speed;//得到鼠标移动X轴距离  
            float mousY = Input.GetAxis("Mouse Y") * roate_Speed;//得到鼠标移动Y轴距离
            target_transform.transform.RotateAround(Sphere1.transform.position, new Vector3(0, -mousX, 0), k);  //new Vector3(0, -mousX, 0)
            target_transform.transform.Translate(new Vector3(0, -mousY, 0));
        }
        else
        {
            Debug.Log("无法取得对象");
        }
    } 


}

void DestoryCube()
{

        if (Input.GetMouseButtonDown(0))
        {
            Ray ray = ca.ScreenPointToRay(Input.mousePosition);         //鼠标点击的位置发射射线
            RaycastHit hit;                                             
            if (Physics.Raycast(ray, out hit))
            {
                Debug.DrawLine(ray.origin, hit.point);
                GameObject gameobj = hit.collider.gameObject;          //捕捉到射线射到的物体
                GameObject Body = GameObject.Find(cubeAll[i]);        //将字符串实例化成Cube
                if (gameobj.name == cubeAll[i])
                {
                    Body.SetActive(false);
                }
            }
        }

         if (Input.GetMouseButtonUp(0))
       {

       }
}







}

猜你喜欢

转载自blog.csdn.net/qq_25325511/article/details/50418404