How To Disable Physics System In Unity Tutorial | Unity Rigidbody Physics
This tutorial shows how to disable physics for specific objects in Unity by using the Rigidbody component. You'll learn to stop physics interactions (like gravity and collisions) and easily toggle them on or off with simple scripts.
WATCH FULL TUTORIAL ON YOUTUBE
=========================================================
3 Unity Projects Only $50 - ON SALE!!!!
Contact: unitycoderz2022@gmail.com
=========================================================
Script:
DisablePhysicsSystem.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DisablePhysicsSystem : MonoBehaviour
{
private Rigidbody rb;
void Start()
{
// Get the Rigidbody component attached to the GameObject
rb = GetComponent<Rigidbody>();
// Check if Rigidbody is attached
if (rb == null)
{
Debug.LogError("Rigidbody not found!");
}
}
// Method to disable physics (make the object kinematic)
public void DisablePhysics()
{
if (rb != null)
{
// Make the object Kinematic, which disables physics
rb.isKinematic = true;
// Optionally, you can also freeze its position and rotation if needed
rb.constraints = RigidbodyConstraints.FreezeAll;
}
}
// Method to re-enable physics
public void EnablePhysics()
{
if (rb != null)
{
// Restore normal physics by setting isKinematic to false
rb.isKinematic = false;
// Optionally, remove any constraints if you froze the object earlier
rb.constraints = RigidbodyConstraints.None;
}
}
// Method to toggle between enabling and disabling physics
public void TogglePhysics()
{
if (rb != null)
{
// If the object is kinematic, enable physics, else disable it
if (rb.isKinematic)
{
EnablePhysics();
}
else
{
DisablePhysics();
}
}
}
}