I’m making a first person game where the player explores caves and stuff. I want the player to be able to click a torch on the wall, and then for it to attach to the player/camera (similar to minecraft). Can anyone help me?
It’s easier to fake the picking: have the light previously attached to the camera, but keep it disabled, and have the torch model fixed in the wall. When the torch model is clicked, destroy it and activate the camera light - for instance (camera script):
function Update(){ if (Input.MouseButtonDown(0)){ // if left mouse button pressed... var hit: RaycastHit; var ray = camera.ScreenPointToRay(Input.mousePosition); // cast a ray from the mouse pointer to 4 meters ahead if (Physics.Raycast(ray, hit, 4)){ // if the object clicked is tagged "Torch"... if (hit.transform.tag == "Torch"){ Destroy(hit.transform.gameObject); // destroy the clicked object... light.enabled = true; // and activate the camera light } // you can pick other objects here } } }
Remember to:
1- add the light to the camera: select the camera and click Component/Rendering/Light, then select light Type as Directional in the Inspector, and clear its checkbox to let it initially disabled;
2- attach the script above to the camera;
3- tag the torch model as “Torch” - since this isn’t a default tag, you must register it in menu Edit/Project Settings/Tags.