Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
1
Question by mbhigham · Oct 12, 2016 at 04:06 PM · lerpmovinggameobject

How do I make a object move between two positions? and whats wrong with my code.

I have two empty game objects that are acting as my trigger points, wp1 (Waypoint 1) and wp2 (Waypoint 2), this is my code.

It's moving from wp1 to wp2 with 2 seconds then jumping straight back to wp1 and repeating again.

I need to move the object between wp1 and wp2 so move from wp1 to wp2 then move back to wp1 and so on.

 #pragma strict
 
     // Transforms to act as start and end markers for the journey.
     var startMarker: Transform;
     var endMarker: Transform;
 
     // Movement speed in units/sec.
     var speed = 2.0;
 
     // Time when the movement started.
     private var startTime: float;
 
     // Total distance between the markers.
     private var journeyLength: float;
 
     //How much distance has been covered so far
     var fracJourney : float;
 
     // Follows the target position like with a spring
     var distCovered : float;
 
 
 function Start () 
 {
 
     // Keep a note of the time the movement started.
     startTime = Time.time;
         
     journeyLength = Vector3.Distance(startMarker.position, endMarker.position);
 
     distCovered = (Time.time - startTime) * speed;
 }
 
 function Update () {
 
     // Distance moved = time * speed.
     distCovered = (Time.time - startTime) * speed;
         
     // Fraction of journey completed = current distance divided by total distance.
     fracJourney = distCovered / journeyLength;
         
     // Set our position as a fraction of the distance between the markers.
     transform.position = Vector3.Lerp(startMarker.position, endMarker.position, fracJourney);
 }
 
 function OnTriggerEnter(objCollider : Collider)
 {
     if (objCollider.gameObject.name == "wp1")
     {
         transform.position = Vector3.Lerp(startMarker.position, endMarker.position, fracJourney);
     }
     else if (objCollider.gameObject.name == "wp2")
     {
         transform.position = Vector3.Lerp(endMarker.position, startMarker.position, fracJourney);
     }
 
 }

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

1 Reply

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

Answer by TBruce · Oct 12, 2016 at 08:07 PM

You can do it with the following script

 #pragma strict
 
 // Transforms to act as start and end markers for the journey.
 var startMarker: Transform;
 var endMarker: Transform;
 
 // Movement speed in units/sec.
 var speed = 2.0;
 
 // the wait time between waypoints
 var waitTimeBetweenWaypoints : float = 0.25;
 
 function Start () 
 {
     Patrol();
 }
 
 function Patrol()
 {
     while (true)
     {
         var time : float = 0;
         var lerpValue : float;
 
         // set the position of to the startMarker) 
         transform.position = startMarker.position;

         // move object from startMarker to endMarker) 
         while ((transform.position != endMarker.position) && (time < speed))
         {
             time += Time.deltaTime;
             lerpValue = (time / speed);
             transform.position = Vector3.Lerp (startMarker.position, endMarker.position, lerpValue);
             yield;
         }
         // set the position of to the endMarker) 
         transform.position = endMarker.position;
         yield WaitForSeconds(waitTimeBetweenWaypoints);
 
         time = 0;
         // move object from endMarker to startMarker) 
         while ((transform.position != startMarker.position) && (time < speed))
         {
             time += Time.deltaTime;
             lerpValue = (time / speed);
             transform.position = Vector3.Lerp (endMarker.position, startMarker.position, lerpValue);
             yield;
         }
         transform.position = startMarker.position;
         yield WaitForSeconds(waitTimeBetweenWaypoints);
     }
     yield;
 }

Here is a Unity package with a demo scene so you can see how it works.


patroltest.zip (6.4 kB)
Comment
Add comment · Show 4 · 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 TBruce · Oct 19, 2016 at 08:03 PM 0
Share

@mbhigham Did you find this answer helpful?

avatar image mbhigham TBruce · Oct 20, 2016 at 03:31 AM 0
Share

Yes! Worked perfectly thank you! I did upvote the reply sorry for no feedback but worked amazing!

avatar image mbhigham TBruce · Oct 20, 2016 at 03:33 AM 0
Share

Do you know a script for making this same thing but with multiple waypoints? so the AI goes to set waypoints like a patrol route?

avatar image TBruce mbhigham · Oct 28, 2016 at 08:04 PM 0
Share

Sorry, just saw this question. This can definitely be done. Could you pleas post this in a separate question? You can make sure that I am notified by placing @$$anonymous$$avina in the question.

But before you do, might I suggest looking in the Asset store? Here are two good way point systems (written in C#) that are free to download

  1. Rapid Waypoint System

  2. Waypoint Pro 2D

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

76 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 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 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

Game object position is stuck when using mathf.sin,Object's position stuck when using Math.sin 0 Answers

Tilting a camera around an object 0 Answers

Acces shader color and lerp 1 Answer

Help with recttransform lerp , lags on mobile not on pc. 1 Answer

Lerp to Gaze Location Smoothing Motion Issues (Cardboard) 0 Answers


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