Continuous movement in Update class

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?

bool isTranslating;

void Update ()
{   
    if (Input.GetKey(KeyCode.Space))
        isTranslating = true;
    
    if (isTranslating)
        transform.Translate(direction * cubeSpeed * Time.deltaTime);
    
    transform.Rotate(randomSpin * angularSpeed * Time.deltaTime, Space.World);
}

This should work. If you haven’t learned about the bool type yet, I’d suggest you do.

Hello Arumiat,

It sounds like you want to use booleans.

public class CubeMove : MonoBehaviour {
 
     public float cubeSpeed = 5f;
     public Vector3 direction;
     public float angularSpeed = 5f;
	 public bool Pressed; // Created this Boolean.
 
 
     // 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.GetKeyDown(KeyCode.Space)) // If the user presses Space Once.
         {
			Pressed = !Pressed; // It will toggle between the booleans.
		 }
		 
		 if (Pressed == true) // 
         {
             Debug.Log("space was pressed");
             transform.Translate(direction * cubeSpeed * Time.deltaTime);
         }
		 
		 if (Pressed == false) 
         {
            //Stop Direction Code here.
         }
        
         transform.Rotate(randomSpin * angularSpeed * Time.deltaTime, Space.World); // I don't know what this is for, I'll ignore that.
 
     }
 }

I hope this helps.