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 /
avatar image
0
Question by Kaitocs · Oct 04, 2016 at 05:12 AM · updateienumeratormove an objectvector3.lerpwhile loop

How to move GameObject after 1 second c#

Hi guys, I actually have this situation where there is a cube, when I place it a structure comes up from the ground using Vector3.Lerp and stops. I want to make the structure to start after 1 second, but I'm having problems since with this script it only moves 1 frame (i think it's because ienumerator dosn't update or stuff. I read on other answers that I should use a while loop but while trying I freezed Unity once and if I add break; to avoid freezes the gameobject only moves by 1 frame. Any ideas? Here's the script in c# (thank you in advance guys!):

 using System.Collections;
 
 public class WinScript : MonoBehaviour {
 
     public GameObject wall;
     private Vector3 startPos;
     private Vector3 endPos;
     private float distance = 16.82f;
 
     private float lerpTime = 6;
     private float currentLerpTime = 0;
 
     private bool triggerEnter = false;
     // Use this for initialization
     void Start () {
 
         startPos = wall.transform.position;
         endPos = wall.transform.position + Vector3.up * distance;
     }
 
     void OnTriggerEnter(Collider other)
     {
         if (other.transform.tag == "Cube") 
         {
             triggerEnter = true;
             StartCoroutine ("Portal");
         } 
 
 
     }
 
     // Update is called once per frame
     IEnumerator Portal () {
         
         if (triggerEnter == true) {
 
             currentLerpTime += Time.deltaTime;
 
             if (currentLerpTime >= lerpTime) 
             {
                 currentLerpTime = lerpTime;
             }
 
             float Perc = currentLerpTime / lerpTime;
 
             yield return new WaitForSeconds (1);
             while (true) {
                 wall.transform.position = Vector3.Lerp (startPos, endPos, Perc);
                 break;
             }
         }
     
     }
 
 }
 
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

Answer by iabulko · Oct 04, 2016 at 06:37 AM

When you want to make coroutine work like Update() all you need is to just add a

 yield return new WaitForEndOfFrame();

and there is no need for break then (although is should break at some point. Try this:

  while (Perc <= 1) {
     wall.transform.position = Vector3.Lerp (startPos, endPos, Perc);
     yield return new WaitForEndOfFrame();
 }

PS: C# convention is to name variables startiing from small letter, so you should name Perc variable perc.

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 ScaniX · Oct 04, 2016 at 09:40 AM 0
Share

And... perc needs to get modified inside of the while. :)

avatar image
0

Answer by doublemax · Oct 04, 2016 at 06:41 AM

I think the Coroutine should look like this:

 if (triggerEnter == true)
  {
    yield return new WaitForSeconds (1);
    
    float elapsed = 0.0f;
 
    while ( elapsed < lerpTime ) {
      wall.transform.position = Vector3.Lerp (startPos, endPos, elapsed / lerpTime);
      yield return null;
    }
    wall.transform.position = endPos;
  }

Untested, but you should get the idea.

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 ScaniX · Oct 04, 2016 at 09:39 AM 0
Share

elapsed should get modified inside of the while. :)

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

6 People are following this question.

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

Related Questions

Vector3.Lerp snaps when called too frequently. 2 Answers

Move player to mouse position never go to exact position 0 Answers

Problem with while loop in Coroutine 2 Answers

Problems with While Loops Only repeating Once 1 Answer

IEnumerator in Update 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