RigidBody vs Transform and Update vs FixedUpdate in C# for Unity

Karuna Ketan
2 min readDec 16, 2022

--

I learned about RigidBody vs Transform and Update vs FixedUpdate in Unity…

12th Program:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
// Move using Rigidbody
Rigidbody2D _rb;
float _inputHorizontal;
float _moveSpeed = 10f;
Vector2 _currentVelocity;

// Move using transform
float _moveSpeedTransform = 10f;

// Start is called before the first frame update
void Start()
{
// Move using Rigidbody
_rb = GetComponent<Rigidbody2D>();
}

// Update is called once per frame
void Update()
{
// Move using Rigidbody
_inputHorizontal = Input.GetAxisRaw("Horizontal");
_currentVelocity = new Vector2(_inputHorizontal * _moveSpeed, 0f);

MovePlayerTransform();
}

void FixedUpdate()
{
// MovePlayerRigidBody();
}

void MovePlayerTransform()
{
if (Input.GetKey(KeyCode.LeftArrow))
{
transform.Translate(new Vector2(-_moveSpeedTransform * Time.deltaTime, 0f), Space.Self);
}
else if(Input.GetKey(KeyCode.RightArrow))
{
transform.Translate(new Vector2(_moveSpeedTransform * Time.deltaTime, 0f), Space.Self);
}
}

void MovePlayerRigidBody()
{
if (_inputHorizontal != 0)
{
_rb.velocity = _currentVelocity;
}
else
{
_currentVelocity = new Vector2(0f, 0f);
_rb.velocity = _currentVelocity;
}
}
}

RigidBody vs Transform:

  • Even though you do not use physics for your character and you move your character with transform with a collider attached, you still have to attach the rigidbody component to it since if you don’t, Unity will see your character as a static collider instead of a dynamic collider, which causes Unity to have to update the caches every frame for the volume of the static collider because it assumes static colliders do not move.

Update vs FixedUpdate:

  • Update runs once per frame. FixedUpdate can run once, zero, or several times per frame, depending on how many physics frames per second are set in the time settings, and how fast/slow the framerate is…

That’s all…Thank you for reading😉:)

click here to follow me in LinkedIn for more updates…

click here to follow me in GitHub for more updates…

--

--

Karuna Ketan

Game Designer and Developer | Unity, Unreal Engine, Blender