Search Unity

Detecting slope

Discussion in 'Scripting' started by QualixInteractive, Jul 17, 2014.

  1. QualixInteractive

    QualixInteractive

    Joined:
    Nov 2, 2013
    Posts:
    9
    Hey guys,

    I am working on an endless runner (sidescrolling) and my character has a basic movement script that detect ground with raycasts and a simple collision system that works simply as upon colliding with a collider tagged "obstacle", the character will die.

    The issue with this, is that some obstacle has a slope (see picture) and upon getting on it, as I have 3 raycasts downwards, I do stop falling, but as I continue to move to the right I collide with the obstacle and die.

    Is anybody able to help me out with this, I've been trying front raycasts to calculate the angle of the slope and elevate player as he gets on it, but as soon as the first raycast is not in the slope anymore than I stop elevating and many other techniques that did not work...

    Please share your ideas over here, I'll be glad to get advices from you!
     

    Attached Files:

  2. cranky

    cranky

    Joined:
    Jun 11, 2014
    Posts:
    180
    It depends on which vector the angle you want is relative to. Using the RaycastHit, you could do:

    Code (CSharp):
    1. Vector3.Angle(hit.normal, Vector.up);
    That would return how many degrees are between the normal of the RaycastHit and the world up vector.

    Keep in mind the normal of an object is perpendicular to it!