Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Move in relation to surface normal (Project On Plane). Move up / down slopes DOTS [Solved]

Discussion in 'Entity Component System' started by toomasio, Nov 2, 2020.

  1. toomasio

    toomasio

    Joined:
    Nov 19, 2013
    Posts:
    195
    Hello,

    Was about to post a question about this, but ended up getting my answer. Figured I'de share. Apologies if this seems noobish compared to some of the advanced stuff getting posted here.

    Vector3.ProjectOnPlane is:

    Code (CSharp):
    1. public static Vector3 ProjectOnPlane(Vector3 vector, Vector3 planeNormal)
    2. {
    3.      return vector - Vector3.Project(vector, planeNormal);
    4. }
    So you can just use the DOTS math.project to do this same logic.

    This will make it so the input direction's "up" will always be in relation to the surface normal. So when moving a player controller up and down slopes, it might prevent the stair-casing jitters or bounces. Hope this helps some people!

    more info here:
    https://answers.unity.com/questions/1601159/i-dont-understand-vector3projectonplane.html
    https://answers.unity.com/questions/1532790/what-exactly-is-the-vector3projectonplane-document.html
     
    Nyanpas, varnon and florianhanke like this.
  2. Lieene-Guo

    Lieene-Guo

    Joined:
    Aug 20, 2013
    Posts:
    547
    when you are sure planNormal is a valid unit vector this is faster.
    Code (CSharp):
    1. public static float3 ProjectOnPlane(float3 vec, float3 planNormal) => vec - math.dot(vec, planNormal);
    math.project is for project one vector to other when none of them are required to be unit length. so there are some overhead calculation.
     
    BigRookGames, Pegrande12 and toomasio like this.
  3. Lieene-Guo

    Lieene-Guo

    Joined:
    Aug 20, 2013
    Posts:
    547
    Oh. Unity.Physics have a plan struct.
    Unity.Physics\Base\Math\Plane.cs
    It will fit in most of your use case.

    Edit: after a quick check, it does not have vector projection function. but it has point projection to plan funciton
     
    ScriptsEngineer and toomasio like this.
  4. toomasio

    toomasio

    Joined:
    Nov 19, 2013
    Posts:
    195
    Interesting. I'll take a look. Thanks!
     
  5. jdigi78

    jdigi78

    Joined:
    Jun 18, 2014
    Posts:
    3
    This code does not work, and frankly I'm not sure what you were trying to do because the result is not even close to the desired. OP's solution works though.