- Home /
Physics2D: how to add friction?
Hello, I have a Ball sprite and a Floor sprite. The Ball has a Rigidbody 2D and is controlled by the player with this script:
using UnityEngine;
using System.Collections;
public class Ball : MonoBehaviour {
public float xSpeed = 4;
void FixedUpdate () {
float inputX = Input.GetAxis ("Horizontal");
if (inputX != 0)
{
rigidbody2D.AddForce(new Vector2(inputX * xSpeed, 0));
}
}
}
When the Ball moves on the Floor it doesn't decelerate. How can I add friction to the Ball on the Floor so that it decelerates till stopping, after a force has been applied to it?
Thank you in advance! :)
You could add a smaller amount of force in the opposite direction it is moving.
Thank you for your advice. Anyway it seems a bit a hack. Isn't there a more elegant way to achieve friction? Anyway if I use your method, I must add the "deceleration" force the step after the step that adds the movement force, otherwise the ball only gets the resultant force of the "movement" and "deceleration" force. Thank you anyway for your help! :)
Answer by Aram-Azhari · Nov 13, 2013 at 06:20 PM
Create a Physics2D material and then apply that physics material to your floor. As you see in the picture, it allows you to define friction.
Now to make it work the way you want, just change the linear drag of the Ball's rigidbody to something other than 0 and Voila! :)
Thank you for your answer Aram! I have tried applying a Physics2D $$anonymous$$aterial to the Floor sprite, but the ball does not decelerate. Probably I'm making a mistake, but I don't know where. Obviously I have tried incrementing the Friction value, even till huge values, but it seems not working. I also tried to apply a Physics2D $$anonymous$$aterial also to the Ball, but after applying a force to the Ball on the Floor, it continues to move without decelerating. I attach my project. my project
Ok, I found it!
First of all, the friction is working fine. Let me illu$$anonymous$$ate:
When you apply a force to the center of the ball, your ball should slip on that floor ins$$anonymous$$d of rotating. The reason it rotates is that the floor has friction, and the reverse force is applied from the floor to the ball. The ball reacts by transferring the force. If you don't believe me, make the friction of the floor physic2d material 0 and see that the ball slides.
If you were to apply the force to the bottom of the ball (exactly at the point it touches the floor), you wouldn't have movement if the floor and ball's frictions were too high.
Now to make it work the way you want, just change the linear drag of the Ball's rigidbody to something other than 0. Voila!
Very nice answer :) I needed this for something else and the linear drag helped solve it! Thanks!
But in my case, that friction is not at all effecting my movement of the object. Once started, the object moves to a distance irrespective of the frictional value..
actor.GetComponent().velocity = new Vector2(movespeed, actor.GetComponent().velocity.y);
This is the code used to move my object - "actor". actor has a rigid body. Also the surface at which actor is placed also have a rigid body. The friction material is added to surface. movespeed is a constant value given. This code is only called once and not in update () function.
Note that isTrigger is not enabled in both. If enabled, the actor fell down. I tried the friction values from 0.0 to 0.9. But no use.