How to correct this issue with a top-down aiming angle in Unity – 2D?
I'm trying to build my first project and I'd like some input for a math correction. The game is a top down 2D shooter, a bit like the iconic "chaos engine" from SNES. The issue is that either the player (controller by mouse position) or the enemy don't aim accurately enough on certain angles. I'm using a formula that translates the coordinates of the target point into a rotation, so the sprite faces the target. The bullets are always shot straight. The problem is that they miss the target in certain orientations... it doesn't seem accurate enough. I've been looking for a best approach but so far I haven't found any. Can anyone help? Thank you in advance! To rotate the enemy I'm using this: Vector3 currentPosition = transform.position; Vector3 nextPosition = target.transform.position; Vector3 directionOfTravel = nextPosition - currentPosition; float angle = (Mathf.Atan2(directionOfTravel.y, directionOfTravel.x) * Mathf.Rad2Deg) - 90; transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
You can try something like this.
float step = speed * Time.deltaTime; Vector3 newDir = Vector3.RotateTowards(transform.forward, directionOfTravel, step, 0.0F); transform.rotation = Quaternion.LookRotation(newDir);
- Database Administration Tutorials
- Programming Tutorials & IT News
- Linux & DevOps World
- Entertainment & General News
- Games & eSport