- Home /
How to define surface properties on a 2d sprite?
I'm trying to emulate moving on grass on Unity 2D (reduce speed of movement). I'm setting a ball's velocity to Vector(3,0), and I'm trying to have a grass tile have friction on it, so that the ball gradually decreases in speed when it overlaps the grass tile.
I've tried creating a new material with friction on it and applying it to the grass tile (using Box Collider), but couldn't see any difference.
Any idea on how it can be achieved?
Thanks :)
Are you setting the velocity every frame, or are you setting it once? If you are setting it every frame, then you are circumventing friction.
Im using. input.Get$$anonymous$$ey to the right arrow in the update function. But Im setting it outside the frictioned tile.
Are you using a rigidbody to control movement or a character controller?
Answer by JustFun · Aug 26, 2014 at 08:00 PM
I assume, that you are using 2D versions of rigidbody and collider. Check IsTrigger checkbox in grass tile collider and attach script to grass tile with following code:
using UnityEngine;
using System.Collections;
public class Friction : MonoBehaviour
{
public float friction = 0.1f; // adjust this value for more realistic behaviour
void OnTriggerEnter2D(Collider2D col)
{
col.rigidbody2D.drag = friction;
}
void OnTriggerExit2D(Collider2D col)
{
col.rigidbody2D.drag = 0;
}
}
Your answer
