I’m making a game where the boss is supposed to come in at one point. Everything works fine save for the player tracking. I added a public Transform field to the boss’ movement script and am trying to get the players transform by linking it in Unity, however when I put it in the Update function it returns the initial transform of my player no matter where I move him.
Here is the code:
public Transform player;
public float fireRate, speed;
Rigidbody2D rb;
void Update ()
{
float dy = transform.position.y - player.transform.position.y;
Debug.Log(transform.position.y + ", " + player.position.y);
if (dy > 0) { rb.velocity = new Vector2(0f, speed); }
else if (dy < 0) { rb.velocity = new Vector2(0f, -speed); }
}
And a screenshot of the editor view:
In the first and second one you can see that the Debug.Log() output is showing the same second number (the players position on the y axis) even though he is in different places, while the first number (the cow’s position on the y axis) has changed.
Is there any way I can continuously track the players transform?