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 Is there a way to move alongside a certain object?

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

  1. davebes123

    davebes123

    Joined:
    Aug 25, 2021
    Posts:
    34
    So far I have a simple zipline but I have to manually input the target position into my inspector with the position that I want to end up at. I know this is a poor way of going about this because if I have multiple ziplines, my inspector will be filled with many separate positions for different ziplines.

    Is there a way to go up the zipline but not have to manually enter the transform.position for the target destination of each zipline?

    Currently I have a long cylinder object with a capsule collider and trigger ON, and my player moves seamlessly to the target destination.

    Code (CSharp):
    1. public Vector3 positionToMoveTo;
    2.  
    3. public void MoveOnZipline()
    4.     {
    5.         StartCoroutine(LerpPosition(positionToMoveTo, 5));
    6.     }
    7.  
    8.     IEnumerator LerpPosition(Vector3 targetPosition, float duration)
    9.     {
    10.         float time = 0;
    11.         Vector3 startPosition = transform.position;
    12.         while (time < duration)
    13.         {
    14.             transform.position = Vector3.Lerp(startPosition, targetPosition, time / duration);
    15.             time += Time.deltaTime;
    16.             yield return null;
    17.         }
    18.         transform.position = targetPosition;
    19.     }
     
  2. Sluggy

    Sluggy

    Joined:
    Nov 27, 2012
    Posts:
    960
    The usual way is to make the Vector3 a transform and then place a GameObject in the scene and link it that way. Now you have an object with gizmos handles that can quickly and easily be adjusted and even parented so that it moves with the whole hierarchy. If you decide you don't want extra GameObjects sitting around (for some reason, some people are terrified to death of them these days) then you could write your own gizmo that lets you place a marker visually in the editor as in local space and then fills in the position data as it's moved.
     
  3. davebes123

    davebes123

    Joined:
    Aug 25, 2021
    Posts:
    34
    Thanks for the reply. With gizmos, are you referring to this? https://docs.unity3d.com/ScriptReference/Gizmos.html

    In that case I'll need to take some time to learn more about them since I don't have any experience using gizmos.

    But what exactly would gizmos do in this case?
     
  4. Sluggy

    Sluggy

    Joined:
    Nov 27, 2012
    Posts:
    960
    Yes, that was what I was referring to. You can use that API to effectively program your own visual handles into the editor for the scene as well as other details. Effectively it allows you to extend the editor to support your own code better. But honestly, I'd go with the quick and dirty method for now and just use an empty gGmeObject and change the position to a Transform. Quick, easy, and it doesn't distract you from the task at hand :)