Hi guys, I’m learning myself scripting and am just having a play with trying to do different things. I’ve made it so that my cube can spin, and move off in a randomized direction.
public class CubeMove : MonoBehaviour {
public float cubeSpeed = 5f;
public Vector3 direction;
public float angularSpeed = 5f;
// Use this for initialization
void Start () {
direction = new Vector3(Random.Range(-1f, 1f), Random.Range(-1f, 1f), 0f);
direction = direction.normalized;
randomSpin = new Vector3(Random.Range(-90f, 90f), Random.Range(-90f, 90f), Random.Range(-90f, 90f));
}
// Update is called once per frame
void Update () {
if (Input.GetKey(KeyCode.Space))
{
Debug.Log("space was pressed");
transform.Translate(direction * cubeSpeed * Time.deltaTime);
}
transform.Rotate(randomSpin * angularSpeed * Time.deltaTime, Space.World);
}
}
I’d like to make it so that if I press my spacebar, the cube will Translate continuously in the random direction I have set for it. Because my if statement is in Update, I have to hold down my Spacebar for the cube to move continuously. I’d like to press it just once. Is the correct technique to do this to use a different KeyDown function to run this every frame thereafter, or do I need to use a coroutine or different class or something?