ā† Back to blog physics

How to add Friction to a Rigidbody in Unity

by Vinod Ravisankar Jan 29, 2024

The concepts of Rigidbodies and Friction are important for developing a physics based game or movement in Unity. Unity simplifies physics with its inbuilt physics engine, making it easier for developers to bring their creative visions to life. In this article we will understand two fundamental physics concepts in Unity: Rigidbodies and Friction, and provide practical insights into their application.

What is a Rigidbody?

In Unity, a Rigidbody is a physics component that, when added to a GameObject, enables it to react to physics logic. This means the GameObject can exhibit properties such as mass, drag, and the ability to react to forces.

A Rigidbody is essential for objects that need to obey the laws of physics, including gravity and collisions with other objects. Without a Rigidbody, a GameObject remains static and unresponsive to physical forces, behaving more like a part of the static scene rather than a dynamic element in the game world.

šŸ“„ Resources:

What is Friction?

Friction is the force that resists the movement of one surface relative to another with which it is in contact. In Unity, this is most commonly observed in the interaction between GameObjects, especially during collisions.

Friction determines how much an object slows down when it slides against another object. It is a crucial aspect in simulating realistic movements and interactions, impacting how players perceive the weight and texture of the game environment.

Why Would You Want Friction on a Rigidbody?

  • Realistic Movement: Friction is vital for realistic movement in games. For example, a character running across different surfaces (like ice, grass, or concrete) should have varying speeds and controls on different surfaces. Without proper friction, every surface would feel the same, breaking the realism of the game.
  • Game Mechanics: Friction can be a pivotal gameplay element. In a puzzle game, for instance, pushing a block across different surfaces with varying friction levels can form the basis of challenging and engaging puzzles.

šŸ“„ Resources:

How to set Friction in Unity

Friction in Unity is primarily set through Physics Materials. Once you create a Physics Material, you can configure the settings on the material to set the static and dynamic friction values.

How to create a Physics Material in Unity

  1. Right click in the Project window.
  2. Click Create > Physics Material.

How to set Friction in the Inspector in Unity

  1. Select the Physics Material.
  2. In the Inspector, adjust the Dynamic Friction and Static Friction values.
  3. Drag the Physics Material onto the Collider component of a GameObject in order to apply the material to a GameObject.

(For 2D, the Physics Material option is available on Rigidbody component.)

How to set Friction using Scripting API in Unity

To set friction dynamically through scripts, follow these steps:

  1. Access the Physics Material in your script.
  2. Modify its dynamicFriction and staticFriction properties.
  3. Assign this material to the material property of the Collider component of your GameObject.

Example code:

using UnityEngine;

public class FrictionAssigner : MonoBehaviour
{
  public Collider objectCollider; // Collider of the GameObject
  public PhysicMaterial frictionMaterial; // Physics Material to be assigned
  public float dynamicFrictionValue = 0.6f; // Default dynamic friction value
  public float staticFrictionValue = 0.6f; // Default static friction value

  void Start()
  {
    // Set the friction values
    frictionMaterial.dynamicFriction = dynamicFrictionValue;

    // Assign the material to the collider
    frictionMaterial.staticFriction = staticFrictionValue; 
    objectCollider.material = frictionMaterial;
  }
}

šŸ“„ Resources:

Conclusion

To sum up, Rigidbodies enable GameObjects to participate in the physics world of Unity, while Friction is key to making their interactions feel real and tangible. You can assign these values in the inspector or using a script depending on your gameplay requirement.

These concepts will help you create a more dynamic and immersive physics game with realistic movement.

Continue reading

Non-static method requires a targetHow to make a countdown timer in UnityPricing Strategy for Indie Game DevelopersUnity: How to Change Your SkyboxHow to add Friction to a Rigidbody in UnityUnity Standard Assets: The Essential GuideTop 10 Recent Games Developed with UnityUnity Sprite MasksHow to move or copy a Unity projectThe Ultimate Guide to Sun and Moon Positioning in Super Simple Skybox for Unity

Build your game with OccaSoftware

Learn on the go. Try OccaSoftware for free, and explore all the tools and assets you need to start, build, and launch your game.

Start now

Try OccaSoftware for free, no commitment required.