Hi,
I have the following scene
I would like to move the pink cube smoothly between the points when a key is pressed. I have the following code:
public class MoveSection : MonoBehaviour {
public Vector3 centre = new Vector3 (0, 0, 0);
public Vector3 right = new Vector3 (0, 0, 4);
public Vector3 left = new Vector3 (0, 0, -4);
void Update() {
if (Input.GetKey(KeyCode.RightArrow)) {
if (transform.position == left) {
//move to middle
Debug.Log ("move to middle");
transform.position = Vector3.Lerp (left, centre, 1);
}
if (transform.position == centre) {
//move to right
Debug.Log ("move to right");
transform.position = Vector3.Lerp (centre, right, 1);
}
if (transform.position == right) {
Debug.Log ("cant move");
}
}
if (Input.GetKey(KeyCode.LeftArrow)) {
if (transform.position == left) {
//cant move
}
if (transform.position == centre) {
//move to left
}
if (transform.position == right) {
//move to middle
}
}
}
}
I’m not even sure if I am lerping correctly!
Thanks for your help,