Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Darkskxcher · Dec 10, 2014 at 03:07 PM · c#positionlerpwaypoints

How do I add multiple points using Lerp?

I want to make multiple waypoints which a gameobject follows and make them random later on. I am currently using the standard lerp snippet but I am absolutely lost as how to add multiple points to the function.

 using UnityEngine;
 using System.Collections;
 
 public class Waypoints : MonoBehaviour
 {
         public Transform startMarker;
         public Transform endMarker;
         public float speed = 1.0F;
         private float startTime;
         private float journeyLength;
         public Transform target;
         public float smooth = 5.0F;
         void Start ()
         {
                 startTime = Time.time;
                 journeyLength = Vector3.Distance (startMarker.position, endMarker.position);
         }
         void Update ()
         {
                 float distCovered = (Time.time - startTime) * speed;
                 float fracJourney = distCovered / journeyLength;
                 transform.position = Vector3.Lerp (startMarker.position, endMarker.position, fracJourney);
         }
 }

Getting this to work has obviously been no problem but I could really use some help adding multiple waypoints between the start- and endMarker.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Baste · Dec 10, 2014 at 03:22 PM

The idea here would be to not have waypoints in between the start and endpoint, as much as having the start and end point being the current last and next waypoint, and switching those when you get to a waypoint

First, you grab an array of the different waypoints you need, and let the start and end marker be private variables:

 private Transform startMarker, endMarker;
 public Transform[] waypoints;

Remember to order this list by the order of waypoints you want. You can use Gizmos to draw lines between consequtive waypoints, that would make it easier to see them.

Anyways, in Start you set the index of which position is your current start point, and set the current start and end markers. I'm setting the markers in an own method, since it's going to be a thing you do a bunch.

 int currentStartPoint;
 void Start() {
     currentStartPoint = 0;
     SetPoints();
 }

 void SetPoints() {
     startMarker = waypoints[currentStartPoint];
     endMarker = waypoints[currentStartPoint + 1];
     startTime = Time.time;
     journeyLength = Vector3.Distance (startMarker.position, endMarker.position);
 }

And in update, switch which waypoints are the first and next when you've reached the endPoint - which is when the lerp value is over 1:

 void Update() {
     float distCovered = (Time.time - startTime) * speed;
     float fracJourney = distCovered / journeyLength;
     transform.position = Vector3.Lerp (startMarker.position, endMarker.position, fracJourney);
     if(fracJourney >= 1f && currentStartPoint + 1 < waypoints.Length) {
         currentStartPoint++;
         SetPoints();
     }
 }

This will move the character from waypoint to waypoint until the last one is reached, at which point the character will stop moving entirely. Hope that helps!

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Darkskxcher · Dec 10, 2014 at 03:42 PM 0
Share

Thank you very much for your fast reply! I am currently trying it out and wanted to tinker with it, but I keep getting the error:

Type UnityEngine.Transform[]' does > not contain a definition for Lenght' and no extension method Lenght' of > type UnityEngine.Transform[]' could be found (are you missing a using directive or an assembly reference?)

I tried a few things but I keep getting some variation or another of the error. Any idea what could solve it?

EDIT: Haha, after quite some googling I found the error! Length was misspelled, I can continue now. Can't believe I didn't notice it!

avatar image nihalsaxena · Oct 04, 2017 at 04:30 AM 0
Share

can i get the same functionality without update() function ?

avatar image
2

Answer by Naphier · May 04, 2016 at 04:53 PM

While the answers here work for the most part, these don't really provide lerp functionality. The power of Linear Interpolation (lerp) is that you can feed in a ratio (0 to 1) of the completeness from start point to end point.

Baste's answer will get you from the start point to the end point, but you can't get back. It also doesn't consider that the waypoints could not be in a straight line (his journeyLength is the end - start... well, what if they're the same point?).

Here's a more robust solution that can handle whatever waypoint positions you feed it AND you can feed in any ratio, making it an actual lerp function. Unfortunately, this comes at a cost, but unless your waypoint list has a lot of waypoints you should be OK. I'll work out an optimized one when I have a chance. Check https://github.com/Naphier/NGTools or https://gist.github.com/Naphier/46e066746ee9eeb70ebfd5062dc426bf for updates.

 using System;
 using UnityEngine;
 
 namespace NG
 {
     public static class Vector3Ext
     {
         #region Multilerp
         // This is not very optimized. There are at least n + 1 and at most 2n Vector3.Distance
         // calls (where n is the number of waypoints). 
         public static Vector3 MultiLerp(Vector3[] waypoints, float ratio)
         {
             Vector3 position = Vector3.zero;
             float totalDistance = waypoints.MultiDistance();
             float distanceTravelled = totalDistance * ratio;
 
             int indexLow = GetVectorIndexFromDistanceTravelled(waypoints, distanceTravelled);
             int indexHigh = indexLow + 1;
 
             // we're done
             if (indexHigh > waypoints.Length - 1)
                 return waypoints[waypoints.Length - 1];
 
             
             // calculate the distance along this waypoint to the next
             Vector3[] completedWaypoints = new Vector3[indexLow + 1];
 
             for (int i = 0; i < indexLow + 1; i++)
             {
                 completedWaypoints[i] = waypoints[i];
             }
 
             float distanceCoveredByPreviousWaypoints = completedWaypoints.MultiDistance();
             float distanceTravelledThisSegment = 
                 distanceTravelled - distanceCoveredByPreviousWaypoints;
             float distanceThisSegment = Vector3.Distance(waypoints[indexLow], waypoints[indexHigh]);
 
             float currentRatio = distanceTravelledThisSegment / distanceThisSegment;
             position = Vector3.Lerp(waypoints[indexLow], waypoints[indexHigh], currentRatio);
 
             return position;
         }
 
         public static float MultiDistance(this Vector3[] waypoints)
         {
             float distance = 0f;
 
             for (int i = 0; i < waypoints.Length; i++)
             {
                 if (i + 1 > waypoints.Length - 1)
                     break;
 
                 distance += Vector3.Distance(waypoints[i], waypoints[i + 1]);
             }
 
             return distance;
         }
 
         public static int GetVectorIndexFromDistanceTravelled(
                 Vector3[] waypoints, float distanceTravelled)
         {
             float distance = 0f;
 
             for (int i = 0; i < waypoints.Length; i++)
             {
                 if (i + 1 > waypoints.Length - 1)
                     return waypoints.Length - 1;
 
                 float segmentDistance = Vector3.Distance(waypoints[i], waypoints[i + 1]);
 
                 if (segmentDistance + distance > distanceTravelled)
                 {
                     return i;
                 }
 
                 distance += segmentDistance;
             }
 
             return waypoints.Length - 1;
         }
         #endregion
 
     }
 }
 


Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image pointcache · Jan 22, 2017 at 06:20 AM 0
Share

Thanks for your work. Great stuff.

avatar image
1

Answer by jenci1990 · Dec 10, 2014 at 04:00 PM

This is a simple way for it:

 using UnityEngine;
 using System.Collections;
 
 public class Waypoints : MonoBehaviour {
     void Start() {
         Vector3[] positions = {new Vector3(1f,1f,0f), new Vector3(0f,0f,0f), new Vector3(0f,1f,1f)};
         StartCoroutine( MultipleLerp(positions, 1f) );
     }
 
 
     IEnumerator MultipleLerp(Vector3[] _pos, float _speed) {
         for (int i = 0; i < _pos.Length; i++) {
             Vector3 startPos = transform.position;
             float timer = 0f;
             while (timer <= 1f) {
                 timer += Time.deltaTime * _speed;
                 Vector3 newPos = Vector3.Lerp(startPos, _pos[i], timer);
                 transform.position = newPos;
                 yield return new WaitForEndOfFrame();
             }
             transform.position = _pos[i];
             startPos = _pos[i];
         }
         yield return false;
     }
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Darkskxcher · Dec 11, 2014 at 02:55 PM 0
Share

Could you maybe explain a little what you did there?

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

30 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Lerp to where the target was x seconds ago? 0 Answers

Move object position on specific axis with Lerp 0 Answers

How do I lerp the position and rotation? 1 Answer

Why does object lerp to above the object it is lerping to? 1 Answer

What is wrong with this script ? if duration is 10 why the transform is moving in 11 seconds and not 10 ? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges