Lock on Enemy Target

I am trying to lock onto a target and shoot while locked.

I have had some sucess with lookAt and using raycast but i can shoot the emeny and the ray works fine but i cant turn away as the lookAt is forcing me back to look at the enemy. i have the lookat on the gun a child of the player.


how about just encapsulating the Lookat function with something like this:

var isTargeting : boolean;

if (isTargeting)

    // place your current targeting code here

then use isTargeting as a flag you set when you turn on targeting.

Hi, Thanks for this it works well.

The only this i want to do extra is when i mouseDn on the object the target is locked on by isTargeting and only when i MouseDown elsewhere the is targeting stops.

So really i want to MouseDn on enemy which lock target on and if i Mouse down elsewhere targeting is off.

Im pretty new at all this but really enjoying it.