Member-only story
Take your game to the next level with realistic movements and interactions using Unity’s physics engine!
Unity Quick Tips: Implementing Basic Physics in Unity (Rigidbody)
Part 3 of Our Bite-Sized Unity Tips and Tricks for Beginners
Welcome to Part 3 of our series of bite-sized Unity tips and tricks for beginners! In this article, we’re going to show you how to implement physics in Unity using rigidbodies. Whether you’re creating a game or another interactive application, physics can add a level of realism and interactivity to your scene. This code assumes you have placed a Rigidbody2D component on your game object.
Here’s the code sample:
using UnityEngine;
public class ApplyPhysics : MonoBehaviour
{
public Rigidbody2D rigidbody;
private void Start()
{
rigidbody = GetComponent<Rigidbody2D>();
}
private void FixedUpdate()
{
rigidbody.AddForce(Vector2.right * 10f);
}
}
In this script, we use the Rigidbody2D
component to add physics to the object. First, we declare a public Rigidbody2D
variable and use the GetComponent
method to get a reference to the component in the Start
function. Then, in the FixedUpdate
function, we use the…