Targetting

Hello there.

I am trying to create a game project for a university investigation, and part of it involves creating an FPS setup where the player can press a button and get the camera to ‘lock’ onto an enemy gameobject in the scene.

I have done the FPS tutorial; I am using it as the basis for the project. I have created currently a gameobject called ‘Lockon’ that I would like to get the camera to align to when the button is pressed.

My thought was to modify the MouseLook script to focus in the direction of the object, but I am both having trouble thinking of how to appropriately make a script that handles which object should the camera be pointed at, as well as how to do the camera math to get it to smoothly point towards the object.

Any help or suggestions would be warmly appreciated. Thank you in advance.

Do you want the camera to move to ‘target’ as well or just look at it?

Just to look at it; the camera is the one that’s hooked onto the player/first person controller.

var target : Transform;
var rotate_damp = 10.0;

function Update () {

	var newRotation = Quaternion.LookRotation(target.position - transform.position);
	transform.rotation = Quaternion.Slerp (transform.rotation, newRotation, Time.deltaTime * rotate_damp);

}

That will look at “target” smoothly and continue looking at it.

Thank you very much for that!

I have that function added to the FPS’ MouseLook script, and it works when I press it; but when I let go of the button, it snaps back to where it was looking originally.

How would I be able to just have the camera (now looking at the target when it is ‘locked’ on) remain looking where it is?