Unity – Move 3D object with Input Keys

In Unity, one of the first things that usually want to do is add some code so that I can move a 3D object with my keyboard keys. I found some ways after experimenting.

On your object, also add the component Rigid Body. Then, create a new script, then drag it to your object so your object is using the script. Now, let’s change the code! My script name is ballchar, so just replace that to whatever you use.

Move object with transform

One way to make the object move is to simply change the position of the object directly with transform.

using UnityEngine;

public class ballchar : MonoBehaviour
{
    const float speed = 0.1f;

    // Update is called once per frame
    void Update()
    {
        Vector3 position = transform.position;

        bool keyUp = Input.GetKey(KeyCode.UpArrow);
        bool keyDown = Input.GetKey(KeyCode.DownArrow);
        bool keyLeft = Input.GetKey(KeyCode.LeftArrow);
        bool keyRight = Input.GetKey(KeyCode.RightArrow);

        if (keyUp)
        {
            position.z += speed;
        }
        if (keyDown)
        {
            position.z -= speed;
        }
        if (keyRight)
        {
            position.x += speed;
        }
        if (keyLeft)
        {
            position.x -= speed;
        }

        transform.position = position;
    }
} 

Now you can move the objects position so it change x, y or z position in the direction of the keys you press down on the keyboard.

Move object with velocity

Another way to make the object move is to change the velocity on the objects Rigid Body.

using UnityEngine;

public class ballchar : MonoBehaviour
{
    const float speed = 0.2f;

    // Update is called once per frame
    void Update()
    {
        Vector3 velocity = GetComponent<Rigidbody>().velocity;

        bool keyUp = Input.GetKey(KeyCode.UpArrow);
        bool keyDown = Input.GetKey(KeyCode.DownArrow);
        bool keyLeft = Input.GetKey(KeyCode.LeftArrow);
        bool keyRight = Input.GetKey(KeyCode.RightArrow);

        if (keyUp)
        {
            velocity.z += speed;
        }
        if (keyDown)
        {
            velocity.z -= speed;
        }
        if (keyRight)
        {
            velocity.x += speed;
        }
        if (keyLeft)
        {
            velocity.x -= speed;
        }

        GetComponent<Rigidbody>().velocity = velocity;
    }
} 

Now you can move the objects velocity so it goes in the direction of the keys you press down on the keyboard! However, if we check the documentation it says this:

Move with CharacterController

The better way I would say is to add a new component to your object called “CharacterController”, here you get some things for free such as the Move method.

Supplies the movement of a GameObject with an attached CharacterController component.

The CharacterController.Move motion moves the GameObject in the given direction. The given direction requires absolute movement delta values. A collision constrains the Move from taking place. The return, CollisionFlags, indicates the direction of a collision: None, Sides, Above, and Below. CharacterController.Move does not use gravity.

using UnityEngine;

public class ballchar : MonoBehaviour
{
    private CharacterController controller;
    private Vector3 playerVelocity;
    private bool groundedPlayer;
    private float speed = 2.0f;
    private float gravityValue = -9.81f;

    void Start()
    {
        controller = GetComponent<CharacterController>();
    }
    // Update is called once per frame
    void Update()
    {
        groundedPlayer = controller.isGrounded;
        if (groundedPlayer && playerVelocity.y < 0)
        {
            playerVelocity.y = 0f;
        }

        Vector3 move = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
        controller.Move(move * Time.deltaTime * speed);

        if (move != Vector3.zero)
        {
            gameObject.transform.forward = move;
        }

        playerVelocity.y += gravityValue * Time.deltaTime;
        controller.Move(playerVelocity * Time.deltaTime);
    }
} 

Note that you might want to check under Rigidbody and Freeze Rotation to stop your objects from rotating.

0 0 votes
Article rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments