Search Unity

Locking Relative Velocity on a Spline

Discussion in 'Scripting' started by dann96, Feb 16, 2018.

  1. dann96

    dann96

    Joined:
    Dec 6, 2014
    Posts:
    113
    What I'm trying to do is create a sort-of 2D Platformer controller that moves on a 2D plane relative to:
    A) where the player is in terms of the spline positioning
    B) the angle at which the Player rests relative to the normal on the ground

    Given the screenshots, I've managed to isolate the relative X,Y, and Z directions that I want, I just can't seem to find the right method to:

    1. Lock the player in place along the X (Red) line
    2. allow relative gravity along the normal (Green) and direction (Blue)) (both relative)

    I'm thinking that it might have something to do with Vector3.ProjectOnPlane, but I don't know the rest.

    Code (CSharp):
    1. lineForward = (spline.GetPoint (spline.CPOB (transform.position, 0, 10, 10000) + 0.01f) - newPos).normalized;
    2.  
    3.         newPos = spline.GetPoint (spline.CPOB (transform.position, 0, 10, 10000));
    4.  
    5.         Debug.DrawRay (newPos, lineForward.normalized,Color.blue);
    6.         Debug.DrawRay (newPos, -Vector3.Cross(Vector3.Cross(currentNormal, lineForward), lineForward).normalized,Color.green);
    7.         Debug.DrawRay (newPos, Vector3.Cross(currentNormal, lineForward).normalized,Color.red);
    demo1.png demo2.png