DEV Community

Atena Razm
Atena Razm

Posted on • Edited on

Character's Movement handling in Unity Input System

To handle the movement of a game object I have 2 ways:

  1. Using Rigidbody Physics
  2. Using Transform for Direct Manipulation

Let's check each of them separately.

1. Using Rigidbody Physics

Pros:

  • Realistic physics interactions with the environment using Unity’s physics engine.
  • Built-in features like gravity, collision detection, and mass which can simplify the implementation of certain behaviors.

Cons:

  • It might add extra complexity and Tuning to get the movement exactly how you want it, for example in the jumping or fraction scenarios.

This approach is usually recommended for a more physics-based or complex 2D game.
There are different ways to control the movement of a Rigidbody:

A. Rigidbody.AddForce

Purpose: Used to apply a force to the Rigidbody.
Behavior: Adds a force to the object, taking into account its mass, resulting in acceleration. Continuously applying force can lead to continuous acceleration.
Use Case: Suitable for simulating realistic physics-based movements, such as jumping, pushing objects, or responding to external forces.

Use it when you want:

  • to simulate forces and acceleration in a more physics-based manner.
  • realistic interactions with other objects, gravity, and forces are desired.
  • to handle scenarios like jumping, falling, or being pushed by external forces.

Code Example:

 float movementInput = playerActionControls.Land.Move.ReadValue<float>();
 float horizontalForce = movementInput * _moveSpeed;
 _rigidbody.AddForce(new Vector2(horizontalForce, 0f));
Enter fullscreen mode Exit fullscreen mode

B. Rigidbody.velocity

v: Velocity vector
Δd: Change in position vector
Δt: Change in time

Here is its formula: Velocity(v)= Δt / Δd

Purpose: Used to directly set the velocity of the Rigidbody.
Behavior: Sets the object's velocity directly, ignoring mass. The object will move at a constant speed in the specified direction, without continuous acceleration unless additional forces are applied.
Use Case: Suitable for situations where you want direct control over the object's speed and direction without simulating realistic physics-based forces.

Use it when you want:

  • direct control over the object's speed and direction without simulating complex physics forces.
  • the object to move at a constant speed without acceleration.
  • to create more straightforward, predictable movements.

Code Example:

 float movementInput = playerActionControls.Land.Move.ReadValue<float>();
 float horizontalMovement = movementInput * _moveSpeed * Time.deltaTime;  
 _rigidbody.velocity = new Vector2(horizontalMovement, _rigidbody.velocity.y);
Enter fullscreen mode Exit fullscreen mode

Note: In this example, Time.deltaTime is used to ensure that the movement is frame-rate independent.

Note: In Unity, when using Rigidbody to handle physics-based movement, it's generally recommended to use either Rigidbody.AddForce or Rigidbody.velocity but not both in the same frame. Mixing them can lead to unexpected behavior because they represent different ways of applying forces to an object.


2. Using Transform for Direct Manipulation

Pros:

  • Simplicity of implementing the basic movements.
  • Predictability of the behavior by having more control over the character’s position.

Cons:

  • Limited physics interactions
  • Manual handling for features like jumping, ground detection, and responding to collisions.

Note: Using transform.Translate for movement is generally better suited for non-physics-based scenarios or when you want to handle movement manually without relying on the physics engine. In such cases, make sure to disable the Rigidbody's gravity and kinematic properties.

This approach is usually recommended for simple 2D platformers with basic movements.
Code Example:

transform.Translate(new Vector3(horizontalMovement, 0, 0));
Enter fullscreen mode Exit fullscreen mode

Top comments (0)