- Home /
Question by
zak666 · Dec 03, 2015 at 12:12 PM ·
scriptingproblem
Player look ad direction?
Hi guys Need a hand with this glitch. I have player in the centre with 4 targets representing left right up down, when u press forward looks at target1. however when you hold forward it cycles through each target when it should only look at target 1 as its assigned to instead of looking at other targets.
u press up, looks at forwards target, you press left looks at left target, but it cycles when you hold down buttons when it shouldn't.
public Transform target1;
public Transform target2;
public Transform target3;
public Transform target4;
void Update ()
{
if (Input.GetKey (KeyCode.UpArrow)) {
transform.Translate (Vector3.right * moveSpeed * Time.deltaTime);
transform.LookAt (target1);
}
if (Input.GetKey (KeyCode.DownArrow)) {
transform.Translate (-Vector3.right * moveSpeed * Time.deltaTime);
transform.LookAt (target2);
}
if (Input.GetKey (KeyCode.LeftArrow)) {
transform.Translate (Vector3.forward * moveSpeed * Time.deltaTime);
transform.LookAt (target3);
}
if (Input.GetKey (KeyCode.RightArrow)) {
transform.Translate (-Vector3.forward * moveSpeed * Time.deltaTime);
transform.LookAt (target4);
}
}
Comment
Best Answer
Answer by Halfbiscuit · Dec 03, 2015 at 12:18 PM
Try using Input.GetKeyDown instead of Input.GetKey.
It will only call this on press rather than hold.
works, but you have to spam the key to make the character move. ill work from here.
and kept rotating anyway
Your answer
![](https://koobas.hobune.stream/wayback/20220612051238im_/https://answers.unity.com/themes/thub/images/avi.jpg)