In my game, I have multiple enemies spawning and flying onto the screen. I’ll post the code below. How would I get the enemies that spawn to turn and fly towards the player character? Apparently adding a rotation to a rigidbody does not go well within Unity. Help is much appreciated!
function spawnEnemy() {
var go : GameObject;
seed = Random.Range(1.0, 6.0);
target = GameObject.FindWithTag("Player").transform;
if (seed == 1)
{
go = Instantiate(enemy[0], Vector3(Random.Range(-4, 2), 14, 178), transform.rotation);
go.AddComponent("Rigidbody");
go.rigidbody.useGravity = false;
go.rigidbody.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - go.rigidbody.position), turnSpeed*Time.deltaTime);
go.rigidbody.AddForce(go.transform.up * -150);
}
if (seed == 2)
{
go = Instantiate(enemy[1], Vector3(Random.Range(-4, 2), 14, 178), transform.rotation);
go.AddComponent("Rigidbody");
go.rigidbody.useGravity = false;
go.rigidbody.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - go.rigidbody.position), turnSpeed*Time.deltaTime);
go.rigidbody.AddForce(go.transform.up * -150);
}
if (seed == 3)
{
go = Instantiate(enemy[2], Vector3(Random.Range(-4, 2), 14, 178), transform.rotation);
go.AddComponent("Rigidbody");
go.rigidbody.useGravity = false;
go.rigidbody.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - go.rigidbody.position), turnSpeed*Time.deltaTime);
go.rigidbody.AddForce(go.transform.up * -150);
}
}