Search Unity

What is the best way to "predefine" a path for an object that the player can control

Discussion in 'Navigation' started by MarcoSiffert, Jan 20, 2018.

  1. MarcoSiffert

    MarcoSiffert

    Joined:
    Sep 2, 2017
    Posts:
    5
    So I wasnt quite sure in where to post this.
    I have the following problem:
    Imagine playing a 2D jump n' run in a 3D world. You can only go left- and right. But the path you walk isnt straight. There are curves. So looking at this image:


    It may be a bit clearer what I want. Simply by clicking the "right" key, my Sphere should move along the black line.
    So I am asking you guys straight away: How would you accomplish this? 1. How do you move the sphere and 2. How do you make it go along the black line. Remember that the sphere should be still able to jump. So there is no restriction in the Y-Axis.

    Thank you guys alot!
     
  2. dadude123

    dadude123

    Joined:
    Feb 26, 2014
    Posts:
    789
    1. Have a bezier path that describes your path so you don't get harsh corners.
    2. Have a variable "distanceOnPath"
    3. When the user presses the key, increment distanceOnPath by velocity*deltaTime;
    4. Every frame position the player at exactly where the distanceOnPath variable says it should be.

    To get jumping as well as "Left-Right" movement (while still continuing walking forwards on the black path) we do some simple vector math.

    Once you have the following line working, you are already 90% done:
    Vector3 forward = Vector3.Normalize(PointOnPath(distanceOnPath+0.0001) - PointOnPath(distanceOnPath));

    Because jumping is not even a complicated thing, it is literally just Vector3.up.
    So that would mean: var finalCharacterPosition = PointOnPath(distanceOnPath) + Vector3.up * currentJumpHeight;

    And for left-rigth movement (aka orthogonal) you can then simply use the previous two directions you already have:

    Vector3 toTheRightOfThePath = Vector3.Cross(forward, up);

    (and for left you'd just multiply by -1)


    tl;dr: use bezier path; get current position based on distance from start; use vector math to get relative directions (left-right) from path.
     
  3. MarcoSiffert

    MarcoSiffert

    Joined:
    Sep 2, 2017
    Posts:
    5
    Thanks for the answer.
    What exactly is 'PointOnPath'? Isnt it just the Players Transform?
     
  4. dadude123

    dadude123

    Joined:
    Feb 26, 2014
    Posts:
    789
    It's a function you write that is supposed to give you a position on the curve depending on how far you have traveled down the path.

    For example if I give it 5, then it would calculate where you would end up if you'd follow the path for 5 units.

    If you don't know how to do this then take a look at the Dolly Path component from the Cinemachine package. It does exactly that. I'm sure you can learn how to do it from the code.

    Every frame, you want to know where on the path your player is.
    So you first need to know how far the player has travelled along the path already, and from that single scalar value, you want a 3D vector that tells you where the player is supposed to be.

    (and then you just add additional movement on top of that, jumping, left-right, ...).

    Not sure if that answers your question... if not ask again :)
     
    JBR-games likes this.