How do I track the initial direction of a Non Rigidbody gameobject in 2D?
Hello! I am struggling to find a way to make my enemy have the same direction after it respawns. It has no rigdbody. I tried to get the initial speed of the all my enemies and put that value in the for of a float array. The problem is that I have the speed value of all my enemies in another script. Would appreciate help. Here is what I tried: ( I only put the essential parts of each scripts) First script:
public GameObject[] enemies;
public float[] InitialDirection { get; set;}
public class Interwithenemy : MonoBehaviour
{
void Start()
{
InitialDirection = new float[enemies.Length];
for (int i = 0; i < enemies.Length; i++){
InitialDirection[i] = Normalenemy.speed[i]; // this line shows error at the last part
}
}
Second Script:
public class Normalenemy : MonoBehaviour{
public float speed;
}
Answer by Chris333 · Apr 06, 2019 at 04:02 PM
I guess that all your enemies have the script called "Normalenemy" as component attached. That should fix your error in the mentioned line.
InitialDirection[i] = enemies[i].GetComponent<Normalenemy>().speed;
But this way you get only the speed value and not any initial direction. A direction is either 2D-Vector or a 3D-Vector.
You should watch some basic tutorial videos to the get an idea of the basic API functions that unity offers. E.g. how to access scripts attached to gameobjects with GetComponent().
Thanks a bunch dude it works perfectly. I will take your advise!