- Home /
Tilting the game object on the slopes
Hi,
I'm making a 2d platformer in which I have slopes. My player should recognize the slopes and tilt accordingly. Is there any effective solution for recognizing the slopes? I have an idea to attach a small empty game object and test it against the land to determine whether he is on slope or not. Is it expensive to do this method? I would love to hear some of your solutions for the problem.
You can think of normal as a ray that is perpendicular to a surface. If you are using a collision such as what you get from OnCollisionEnter(), then you will get back an array of contact points. You could access the normal for the first contact point with:
Vector3 v3Normal = collision.contacts[0].normal;
if you are using a raycast to get your hit point, you would get the normal like:
Vector3 v3Normal = hit.normal;
So I have to shoot ray under my character and get the normal and compare it with angles between them? But how do I get the slope's normal?
public class AlignWithNormal : $$anonymous$$onoBehaviour {
Ray ray = new Ray(Vector3.zero, Vector3.down);
RaycastHit hit;
void Update () {
ray.origin = transform.position;
if (Physics.Raycast (ray, out hit)) {
transform.rotation = Quaternion.FromToRotation (Vector3.up, hit.normal);
}
}
}
Answer by MikeNewall · Feb 23, 2013 at 10:01 PM
You need to fire a ray at the ground and get the surface normal from the hit info. Then you can use the normal direction to set your rotation.
Your answer