unity3d实现3D物体上的点击事件

首先要在摄像机中添加组件Physics Raycaster

void Update () {
    if(Input.GetMouseButtonUp(0)){
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit rayhit;
        if (Physics.Raycast(ray, out rayhit))
        {
            Debug.Log(rayhit.collider.gameObject.name);
        }
    }
}

你可能感兴趣的:(Unity开发)