Implementing Movement, Rotation, and Scaling with Input in Unity

To control an object in Unity, create a new scene and add a Cube. In the Assets folder, establish a Scripts directory and generate a script named PlayerMovement.cs. Attach this script to the Cube.

Define variables for movement speed and direction. In the Update method, capture input from the keyboard using Input.GetAxis for horizontal and vertical axes, update the direction vector, and adjust the transform.position accordingly.

using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    public float moveRate = 5f;
    private Vector3 moveVector;

    void Update()
    {
        float horizontalInput = Input.GetAxis("Horizontal");
        float verticalInput = Input.GetAxis("Vertical");
        
        moveVector.x = horizontalInput;
        moveVector.z = verticalInput;
        
        transform.position += moveRate * Time.deltaTime * moveVector;
    }
}

Multiplying by Time.deltaTime ensures consistent movement per second across varying frame rates. Without it, movement speed would depend on frames per second, leading to inconsistent behavior.

For rotation, use Input.GetKey to detect key presses. Continuously adjust the Y-axis rotation angle when keys are held, applying the change via Quaternion.Euler to transform.rotation.

using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    public float rotationRate = 100f;
    private Vector3 currentRotation;

    void Update()
    {
        if (Input.GetKey(KeyCode.Q))
        {
            currentRotation.y += rotationRate * Time.deltaTime;
            transform.rotation = Quaternion.Euler(currentRotation);
        }
        else if (Input.GetKey(KeyCode.E))
        {
            currentRotation.y -= rotationRate * Time.deltaTime;
            transform.rotation = Quaternion.Euler(currentRotation);
        }
    }
}

Scaling can be controlled with mouse buttons. Use Input.GetMouseButtonDown to detect clicks, modifying transform.localScale to enlarge or reset the object size.

using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    public float enlargeFactor = 2f;
    public float defaultScale = 1f;

    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            transform.localScale = Vector3.one * enlargeFactor;
        }
        else if (Input.GetMouseButtonDown(1))
        {
            transform.localScale = Vector3.one * defaultScale;
        }
    }
}

Key input methods include:

  • Input.GetKey(KeyCode.W): Returns true while the W key is held.
  • Input.GetKeyDown(KeyCode.W): Returns true only on the frame the W key is pressed.
  • Input.GetKeyUp(KeyCode.W): Returns true when the W key is released.
  • Input.GetButton("Jump"): Similar to GetKey for custom-named butttons.
  • Input.GetAxis("Horizontal"): Returns a float from -1 to 1 for AD or arrow keys.
  • Input.GetAxisRaw("Horizontal"): Returns -1, 0, or 1 for discrete input.
  • Input.GetMouseButton(0): Returns true while the left mouse button is pressed.
  • Input.GetMouseButtonDown(1): Returns true on the frame the right mouse button is pressed.
  • Input.GetAxis("Mouse X"): Captures horizontal mouse movement.

Customize button mappings in the Input Manager to assign keys to named actions.

Tags: unity C# game development Input Handling Movement

Posted on Fri, 24 Jul 2026 16:06:59 +0000 by bguzel