- Home /
Aim flashlight at cursor in third person
I have a flashlight object which is parented to the left hand bone of a skinned character rig. I want to set up a raycast from the camera to the cursor and detect where it collides with the terrain (the game is a haunted house project, so it is entirely an interior environment) and aim the transform of the hand bone toward that raycast collision, but I can't seem to get it working. Any help?
Answer by efge · Mar 26, 2011 at 11:08 PM
This script send a ray from the main camera to the mouse position and then rotates the object to the hit point. (Attach it to the object which you want to rotate to the target.)
if (Input.GetButtonDown ("Fire1")) {
var mousehit : RaycastHit;
var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast (ray, mousehit)) {
transform.LookAt(mousehit.transform);
}
}
(It is untested with a bone of a skinned character. Maybe you should use the Head Look Controller and modify it?)
Your answer
Follow this Question
Related Questions
Raycast hits and then doesn't fire again? 2D Game Raycasting Problem 1 Answer
Instantiate same PreFab to an array of GameObject by RayCast. 1 Answer
My Raycasts seem to sometimes miss 0 Answers
Raycast not hitting the collider properly it always has a weird offset 0 Answers
ray cast not working ? 2 Answers