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);
}
}
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.
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);
}
}
}
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
Follow this Question
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