Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Question I have been following the beginner scripting tutorials, but can not get my object to move.

Discussion in 'Getting Started' started by Axolotl13, May 22, 2022.

  1. Axolotl13

    Axolotl13

    Joined:
    May 15, 2022
    Posts:
    2
    Code (CSharp):
    1. public class Movement : MonoBehaviour
    2. {
    3.     public float moveSpeed = 10f;
    4.     void Update()
    5.     {
    6.         if (Input.GetKey(KeyCode.UpArrow))
    7.             transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
    8.     }
    9. }
    I have been following the tutorial that teaches how to rotate and move gameobjects, and it has gotten to the point where you make the object move when a key is pressed. If I remove the if(Input.GetKey... code, it will fly away on its own. However when I add it the object does nothing, even if I press the uparrow. The same input code has worked for projects in the past.
     
  2. pixaware_pwedrowski

    pixaware_pwedrowski

    Joined:
    Oct 25, 2018
    Posts:
    95
    The script you provided works. You can try with a different key, and make sure there is focus on your game area (just click on it to make sure). Also make sure, that the
    moveSpeed
    isn't set to 0 in the inspector
     
  3. pixaware_pwedrowski

    pixaware_pwedrowski

    Joined:
    Oct 25, 2018
    Posts:
    95
    Btw
    Vector3.forward * (moveSpeed * Time.deltaTime)
    will be slightly more efficient
     
  4. Axolotl13

    Axolotl13

    Joined:
    May 15, 2022
    Posts:
    2
    My code is not working.
     
  5. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,102
unityunity