Question by
EddyDavila · Feb 02, 2019 at 11:13 PM ·
transformprefabposition
Problem with Seek Missile and finding enemy prefab new position.
I am making an endless sidescroller. My code spawns enemies from an enemy prefab. The spawned enemy´s position obviously changes as you progress, but my seek missile when fired; always goes to the original enemy prefab position (The one I first placed on the scene), way back at the beginning of the scene and not to the newly spawned enemies position. It seems that my missile code is looking for the enemy prefabs original position and not any of the others. What I want is for the missile to seek the prefabs new position. I have tried deleting prefab from hierarchy so the code looks only for spawned prefabs positions. Can someone guide me a bit, please? Here is some code..
> float distanceToTarget = Vector3.Distance(transform.position, target.position);
if (distanceToTarget < chaseRange)
{
print("distanceToTarget is less than chaseRange");
Vector3 targetDir = target.position - transform.position;
float angle = Mathf.Atan2(targetDir.y, targetDir.x) * Mathf.Rad2Deg - 90f;
Quaternion q = Quaternion.AngleAxis(angle, Vector3.forward);
transform.rotation = Quaternion.RotateTowards(transform.rotation, q, 180f);
transform.Translate(Vector3.up * Time.deltaTime * 30);
transform.localRotation = Quaternion.Euler(0, 180, 0);
}
Comment