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
0
Question by Nikolasio · Mar 09, 2016 at 06:58 AM · coroutinemove object

How to move a GameObject within a Coroutine in C#?

I'm making a short tutorial to show before my game begins.
In this tutorial the number 1(go numberOnePrefab) will blink for 1sec in the center of the screen.
Then a letter (go letterPrefab(Sprite with RigidBody)) will appear in the centre of the screen (so far so good) and has to move to the upper left corner of the screen.
But the letter doesn't move the target position.
Can anyone help me with how I have to move an object within a coroutine?

 using UnityEngine;  
     
 using System.Collections;
     
     public class Tutorial : MonoBehaviour {  
     
     public GameObject     letterPrefab;  
     public GameObject     numberOnePrefab;  
     public float    speed = 1f;
     
     void Start () {  
       
              StartCoroutine(PlaceLetter());  
     }
     
     IEnumerator PlaceLetter() { 
      
     GameObject numberOne =  Instantiate (numberOnePrefab) as GameObject;  
     numberOne.SetActive (true);  
     yield return new WaitForSeconds (1);  
     numberOne.SetActive (false);  
     Destroy (numberOne);  
     
     yield return new WaitForSeconds (1);  
      MoveToTarget ();  
     
     }
     
     void MoveToTarget() {  
     
     GameObject letter = Instantiate (letterPrefab) as GameObject;  
     letter.SetActive (true);  
     Vector3 targetPosition = new Vector3 (-4, -3, -1);  
     Vector3 currentPosition = letter.transform.position;  
     if (Vector3.Distance (currentPosition, targetPosition) > .1f) {  
     Vector3 directionOfTravel = targetPosition - currentPosition;  
     directionOfTravel.Normalize ();  
     letter.transform.Translate (  
                     (directionOfTravel.x * speed * Time.deltaTime),  
                     (directionOfTravel.y * speed * Time.deltaTime),  
                     (directionOfTravel.z * speed * Time.deltaTime),  
                     Space.World);  
     
     }  
     } 










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
1

Answer by Fredex8 · Mar 09, 2016 at 10:51 AM

Having two returns in an IEnumerator possibly isn't a good idea (not really sure). I'd have a second coroutine to wait and then start the MoveToTarget and call that coroutine at the end of PlaceLetter. Something like this:

      void Start () {  
          StartCoroutine(PlaceLetter());  
      }
      
      IEnumerator PlaceLetter() { 
          GameObject numberOne =  Instantiate (numberOnePrefab) as GameObject;  
          numberOne.SetActive (true);  
          yield return new WaitForSeconds (1);  
          numberOne.SetActive (false);  
          Destroy (numberOne);
          StartMove();
      }
      IEnumerator StartMove() { 
          yield return new WaitForSeconds (1);  
          MoveToTarget ();  
      }

I haven't looked at your movement script really though. If you are looking to have it move incrementally you might want to put the movement within a coroutine.

Comment
Add comment · 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
0

Answer by Nikolasio · Mar 09, 2016 at 06:54 PM

Thank you for your reply. Making a second coroutine seems indeed more logical but the letter still doesn't move. I tested the MoveToTarget(),when I put it in the Update () in a script attached to the letterPrefab, the letterprefab moves to the targetPosition. So the MoveToTarget () works. But I don't want to attach a script to the letterPrefab there I'm going to use the letterPrefab in other functions. In my Tutorial script (attached to the Main Camera) I want to get an instance of the letterPrefab and move it the the targetPosition. Is that possible? How? I know that the MoveToTarget() has to be updated every frame and works under Update() in a script attached to the letterPrefab but I don't see how I can implement this in the Tutorial script. Or should I keep the MoveToTarget() attached to the letterPrefab and then when I need the letterPrefab again I disable the MoveToTarget() script?

     using UnityEngine;  
     using System.Collections;
              
      public class Tutorial : MonoBehaviour {  
              
              public GameObject     letterPrefab;  
              public GameObject     numberOnePrefab;  
              public float    speed = 1f;
              
     void Start () {  
                StartCoroutine(PlaceLetter());  
     }
              
     IEnumerator PlaceLetter() { 
               
              GameObject numberOne =  Instantiate (numberOnePrefab) as GameObject;  
              numberOne.SetActive (true);  
              yield return new WaitForSeconds (1);  
              numberOne.SetActive (false);  
              Destroy (numberOne);
              yield return new WaitForSeconds (1);  
              StartMove();  
     }
     IEnumerator StartMove() {    
              yield return new WaitForSeconds (1);    
              MoveToTarget ();  
      }  
 
      void MoveToTarget() {  
  
              GameObject letter = Instantiate (letterPrefab) as GameObject;  
              letter.SetActive (true);  
              Vector3 targetPosition = new Vector3 (-4, 3, -1);  
              Vector3 currentPosition = letter.transform.position;  
              if (Vector3.Distance (currentPosition, targetPosition) > .1f) {  
              Vector3 directionOfTravel = targetPosition - currentPosition;  
              directionOfTravel.Normalize ();  
              letter.transform.Translate (  
                              (directionOfTravel.x * speed * Time.deltaTime),  
                              (directionOfTravel.y * speed * Time.deltaTime),  
                              (directionOfTravel.z * speed * Time.deltaTime),  
                              Space.World);  
              
              }  
     } 
     }





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 Nikolasio · Mar 10, 2016 at 07:51 AM 0
Share

I' ve found a solution that looks like this.

    void Start () {  
                StartCoroutine(PlaceLetter());  
         }  
     
             IEnumerator PlaceLetter() {  
     
             GameObject numberOne = Instantiate (numberOnePrefab) as GameObject;
             numberOne.SetActive (true);
             yield return new WaitForSeconds (1);
             numberOne.SetActive (false);
             Destroy (numberOne);
             yield return new WaitForSeconds (1);
     
             GameObject letter = Instantiate (letterPrefab) as GameObject;  
             float   speed = 1f;    
             Vector3 targetPosition = new Vector3 (-4, 3, -1);  
             Vector3 currentPosition = letter.transform.position;  
             while (Vector3.Distance (currentPosition, targetPosition) > 0.1f) { 
                 letter.transform.position = Vector3.Lerp (letter.transform.position, targetPosition, speed * Time.deltaTime);  
     
                 yield return null;
     
     
             }
     
     
         }

I also changed the code for the movement of the letter (thx Unity!). Thx Fredex8, your reply made me thinking and testing. Best regards Nikola

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Changing transform position - from instantaneous to smooth - not sure how. 0 Answers

Execute coroutine in Update() 8 Answers

yield ends method 0 Answers

MoveTowards and Lerp not working 1 Answer

Can I check if this.gameObject was destroyed? 5 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