Hello, i want to make a game that features a spaceship that teleports to the sides where you need to dodge different objects.
But i experienced troubles with the movement. I want it to be so that while you hold the arrow keys to the left or right the spaceship teleports and stays at that position untill you let go of the arrow key and then it returns to the middle.
But i can’t find a way to do that. The script i have right now makes the spaceship teleport back instantly after the spaceship takes it position to the right or left.
this is my code right now.
using UnityEngine;
using System.Collections;
public class MovementUP : MonoBehaviour {
new public GameObject Rocket;
new Vector3 StartPos = new Vector3 (0, -3, 0);
new Vector3 RightPos = new Vector3 (6,-3,0);
new Vector3 LeftPos = new Vector3 (-6,-3,0);
// Use this for initialization
void Start () {
Rocket.transform.position = StartPos;
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown (KeyCode.D)) {
Rocket.transform.position = RightPos;
} else if (Input.GetKeyDown (KeyCode.A)) {
Rocket.transform.position = LeftPos;
}
else if ((Input.GetKeyDown(KeyCode.A) == false ) & (Input.GetKeyDown(KeyCode.D) == false))
{
Rocket.transform.position = new Vector3 (0,-3,0);
}
}
}
So what can i do to make it stay to the sides untill i let go of the key?