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 CodingNoob · Feb 20, 2015 at 03:13 PM · lerpmovinggameobject

Moving an object constantly between two points with a time delay inbetween

Hi,

I'm currently freaking out about an issue:

I want to make a moving obstacle for my infinite Runner, which shall just constantly move between two positions and each time it reaches one position, it shall wait for a short moment before it goes back. (similar to a laserbeam which switches on and off in a specific interval, the object shall move in a specific interval)

I tried for hours to get Vector3.Lerp to work but it doesn't and I couldn't find any sufficient solution here on answers, although there are a lot similar questions to it.

Maybe someone could tell me what I'm missing, because this should be a really simple task but it's driving me crazy right now....

here is the "latest" code I tried, which did not work:

 using UnityEngine;
 using System.Collections;
 
 public class MovingObstacle : MonoBehaviour {
 
     private Vector3 obstaclePosition;
 
     public bool flyingObstacle;
     public float movingSpeed;
     public float interval = 0.5f;
     public float xMax;
     public float xMin;
     public float yMax;
     public float yMin;
     private bool onPosA = true;
 
     private Vector3 posA;
     private Vector3 posB;
 
     private float xStart;
     private float xEnd;
 
     void Start ()
     {
         Debug.Log ("SPAWNED!!");
 
         SetPositions ();
 
     }
 
     void FixedUpdate()
     {
 
     }
 
     void Update()
     {
         SwitchPositions ();
     }
 
     void SetPositions ()
     {
         xStart = obstaclePosition.x + Random.Range (xMin, xMax);
         xEnd = obstaclePosition.x + Random.Range (xMin, xMax);
 
         posA = new Vector3 (xStart, yMin, 0);
         posB = new Vector3 (xEnd, yMax, 0);
     }
 
     void SwitchPositions ()
     {
         if (onPosA == true)
         {
             transform.position = Vector3.Lerp (posA,  posB, 1);
 
             if(transform.position == posB)
             {
             onPosA = false;
             }
         }
         if (onPosA == false)
         {
             transform.position = Vector3.Lerp (posB,  posA, 1);
             if(transform.position == posA)
             {
                 onPosA = true;
             }
         }
     }
 }


Thanks in advance!

Best regards

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

2 Replies

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

Answer by Berenger · Feb 20, 2015 at 04:41 PM

First, take a look at the documentation for the lerp function : http://docs.unity3d.com/ScriptReference/Vector3.Lerp.html.

You'll see that it takes two Vector3 and a percentage. 0 means the first, 1 the second and .5 the middle.

Now, there is many ways to achieve what you want, here is one using coroutines :

 private IEnumerator Start()
 {
     // Loops each cycles
     while( Application.isPlaying )
     {
         // First step, travel from A to B
         float counter = 0f;
         while( counter < travelDuration )
         {
             transform.position = Vector3.Lerp (posA, posB, counter / travelDuration);
             counter += Time.deltaTime;
             yield return null;
         }
 
         // Make sure you're exactly at B, in case the counter 
         // wasn't precisely equal to travelDuration at the end
         transform.position = posB;
         
         // Second step, wait
         yield return new WaitForSeconds(waitDuration);
 
         // Third step, travel back from B to A
         float counter = 0f;
         while( counter < travelDuration )
         {
             transform.position = Vector3.Lerp (posB, posA, counter / travelDuration);
             counter += Time.deltaTime;
             yield return null;
         }
 
         transform.position = posA;
         
         // Finally, wait
         yield return new WaitForSeconds(waitDuration);
     }
 }
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 CodingNoob · Feb 27, 2015 at 09:49 AM 0
Share

Awesome, thank you very much!!

avatar image
0

Answer by Nymisu · Feb 20, 2015 at 03:40 PM

This is a simple delay system using Time.time:

 private float waitamount = 2f;//in seconds
 private float finishedmovingtime = -waitamount; //this comes to a flat 0. So it can start without waiting for waitamount
 
 void SwitchPositions ()
 {
 if(finishedmovingtime + waitamount < Time.time) //if it took enough time since last movement...
 {
 if (onPosA == true)
   {
   transform.position = Vector3.Lerp (posA, posB, 1);
   if(transform.position == posB)
     {
     onPosA = false;
     finishedmovingtime = Time.time;
     }
   }
   if (onPosA == false)
   {
   transform.position = Vector3.Lerp (posB, posA, 1);
   if(transform.position == posA)
     {
     onPosA = true;
     finishedmovingtime = Time.time;
     }
   }
 }
 }
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 CodingNoob · Feb 20, 2015 at 04:27 PM 0
Share

thanks for the help with the delay, but the most important issue still is there....how to move the object between those two points constantly? right now I'm shooting the object somewhere in to nirvana in the vector's direction and it doesn't stop at the designated position...

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

21 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

Related Questions

How to switch 2 objects' positions gradually? 1 Answer

Moving Maincamera slowly to another position 0 Answers

rigidbody.velocity vs lerp efficiency 0 Answers

lerp from a to moving position 0 Answers

Mathf.Lerp not working 2 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