Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Creating a double sided zipline?

Discussion in 'Scripting' started by davebes123, Mar 18, 2023.

  1. davebes123

    davebes123

    Joined:
    Aug 25, 2021
    Posts:
    34
    I have two cube objects placed at point A and point B with a cylinder object (capsule collider attached and trigger ON) acting as the zipline. I have a basic script attached to the cylinder where if I press a key it will get the player movement script, and a coroutine starts.

    Code (CSharp):
    1.  
    2. // Player movement snippet
    3. public Vector3 positionToMoveTo;
    4.  
    5. public void MoveOnZipline()
    6.     {
    7.         StartCoroutine(LerpPosition(positionToMoveTo, 5));
    8.     }
    9.  
    10.     IEnumerator LerpPosition(Vector3 targetPosition, float duration)
    11.     {
    12.         float time = 0;
    13.         Vector3 startPosition = transform.position;
    14.         while (time < duration)
    15.         {
    16.             transform.position = Vector3.Lerp(startPosition, targetPosition, time / duration);
    17.             time += Time.deltaTime;
    18.             yield return null;
    19.         }
    20.         transform.position = targetPosition;
    21.     }
    There are several issues, but the main problem is that I have to manually add the destination via Vector3 positionToMoveTo (point B is the destination) to my player inspector. With this, my character moves along the zipline and reaches the destination just fine.

    But if I want to come back down, I need to add yet another Vector3 to my player inspector which will just clutter it and if I decide to add multiple ziplines to my game, my inspector will be full of positions which I know is not the way to do this.

    I thought Lerp was the solution here with a coroutine but it prevents me from going both down and up the zipline. Is there another method to solve this (I'm using the in-built CC)? I have been trying to use the direction my player is facing to just move until it reaches the end of the zipline object but I haven't got that to work.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,517
    Ziplines are just a special form of a path, or list of waypoints.

    Yes, you could double-author each one into two ziplines, one each way, but I agree, that's gross.

    When I did my waypointing I used this approach:

    https://gist.github.com/kurtdekker/c2246d3780b93fe4e023695eb947e85d

    Check out the
    CloneToInversePath
    boolean... if checked then at runtime it clones a waypoint list, reverses it, and resubmits it as yet another possible path.

    This way you would author a zipline once but when the game runs there would be two ziplines, one to take you from A to B, one to take you from B to A.

    ... and best of all, no other code changes!
     
  3. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,615