I’m getting crazy…it’s about 5 hours i’m on this stupid problem:
I’m making a 2d games. With bullets.
I want the bullets to follow the player.
Like a tank in a war game, they continue to go forward, and constantly rotate to match the player orientation.
My code is the following, ant it is attatched to the projectile:
//Look the rotation between the player and the projectile
Quaternion targetRotation = Quaternion.LookRotation(player.position - transform.position, Vector3.forward);
//rotate constantly the projectile based on the rotation previously calculated
gameObject.transform.rotation = Quaternion.Slerp(gameObject.transform.rotation, targetRotation, Time.deltaTime * 2);
//at last, it moves the object (in the same direction, but it should change depending on his rotation
gameObject.transform.Translate(Vector3.right * Time.deltaTime * speed);
I tried everything, incluing changing Vector3.forward in the first line, and Vector3.right in the latest, but nothing…the projectiles change their Z axis (even if it is 0 because it’s a 2d games), they rotate on their Y axis…i don’t know what to do, the same exact code worked in other projects…