1. 程式人生 > >unity3d實現3D物體上的點選事件

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);
        }
    }
}