- Home /
Movement while Looking At an enemy
function Update(){ var hit : RaycastHit; var fwd = transform.TransformDirection(Vector3.forward);
if (Physics.Raycast (transform.position, fwd, 100)) {
if (target.gameObject.tag == "Enemy"){
transform.LookAt(target);
}
}
}
I have this script that allows my character to Lock on to an enemy he's facing but he can no longer move left or right and can't rotate as long as he's within range. So I need to ask for 2 things:
1.) A method of free movement while locking onto the target.
2.) A method of breaking the raycast via button press (Disable the lock on feature)
Any help will be appreciated.
Answer by Atnas1010 · Nov 02, 2010 at 05:33 PM
1) I don't see why the character can't move left or right, while he is locked on. As for the rotate part, that is because you force him to look at the target, so naturally he cant turn.
2) You could edit your script line to this (you would have to declare toggle in the beginning of the script)
if (Input.GetKeyDown(KeyCode.Space))
toggle = !toggle;
if (toggle && Physics.Raycast (transform.position, fwd,100)) {
Does this make it work like you want it to?
$$anonymous$$eyCode doesn't go there, a string does, but even with those corrects it doesn't seem to.
As for the rotate, I meant it moves around the locked on enemy, but that was my miswording.
Are you looking something the user should hold down all the time to keep the raycast disengaged, or like a toggle raycast? (I wrote that code to do the first option, but I see now the error. Try with GetButton ins$$anonymous$$d of GetButtonDown. Now that you have explained yourself a little better, I don't understand question 1)
Thee ray cast is always on. I need a way to break it for awhile....on second thought, let's go with the toggle. Set up a Boolean to compare?
Yup, a boolean is always nice :) Edited my post to show how I'd do it.
Your answer
Follow this Question
Related Questions
Rotate character to the moving direction problems? 2 Answers
How do I get GameObjects to look at me? 2 Answers
Moving a wheel character 0 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Look Where You're Going 3 Answers