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