Hello all. I am new to game development, and have been playing around with some concepts I’ve learned so far. I am having an issue instantiating a prefab that has a transform.Lookat() method. When I manually place one of these prefabs, it performs as intended, following my target, but when I instantiate them at runtime they don’t follow the target. The rest of the script works (they also instantiate projectiles and propel them forward), just not the LoookAt() method. Do I not understand how this method works?
Here are the pertinent bits:
void Update () {
time += Time.deltaTime;
transform.LookAt(target.transform);
if(time > 2.0) {
time = 0;
GameObject p = (GameObject)Instantiate(projectile, transform.position, Quaternion.identity);
p.GetComponent<Rigidbody>().AddForce(transform.forward * force);
Destroy(p, 3);
}
}
This controls the launcher object, which creates a projectile and fires it at the target.
void nextLevel() {
Vector3 randV31 = new Vector3(Random.Range(-35.0f, 35.0f), 1.50f, Random.Range(-35.0f, 35.0f));
Vector3 randV35 = new Vector3(Random.Range(-35.0f, 35.0f), 1.50f, Random.Range(-35.0f, 35.0f));
transform.position = GameObject.Find("start").transform.position;
Instantiate(scoreSphere, randV31, Quaternion.identity);
Instantiate(launcher, randV35, Quaternion.identity);
}
This bit of script is attached to the player and when a certain condition is met, it instantiates the launcher object, which is the prefab in question.
Any help you could provide would be very appreciated.