Facebook

How to add Friction to a Rigidbody in Unity

by Vinod Ravisankar Published January 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.

This is a series on Unity Physics. We recommend reading the series in order.

  1. Unity Physics
  2. Unity Collision Detection
  3. Character Controller and Input Systems
  4. Rigidbody Mass in Unity
  5. How to add Friction to a Rigidbody
  6. Cylinder Collider in Unity
  7. Box Collider in Unity
  8. Box Cast in Unity
  9. Sorting Layers
  10. Get the distance between two objects
  11. How to normalize a vector

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 their Unity 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.

Free download: Indie Game Marketing Checklist

Download now

Category

physics

Don't forget to share this post!

Popular assets for Unity

See all assets ->
    Cutting-edge volumetric fog and volumetric lighting with support for transparent materials.
    Volumetric clouds, day night cycles, dynamic skies, global lighting, weather effects, and planets and moons.
    A lightweight procedural skybox ideal for semi-stylized projects.
    Image-based Outlines for 2D and 3D games with variable line weight, color, and displacement options.
    Per-pixel gaussian blur on your entire screen, part of the UI, or in-scene objects.
    Drag-and-drop ready-to-use ambient, impact, and spell particle effects.

Free Indie Game Marketing Checklist

Learn how to make your game successful with this handy checklist.

Download for free