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);

enter image description here

  • \$\begingroup\$ Where is (0,0) on your sprite? If it isn't exactly in the center of your sprite then your rotation will be off. Are you sure the red line is really starting from the center of the model and not the right edge? \$\endgroup\$ – Dunk Jul 14 '16 at 23:01
  • \$\begingroup\$ you gave me the good input! actually it wasnt 0.0.0, but I had to place the shotspawn at the exact position of the gun. now its fine! merci cher ami! \$\endgroup\$ – Denis Taquet Jul 23 '16 at 0:31
  • \$\begingroup\$ Post that as an answer @Dunk or yourself Denis. \$\endgroup\$ – Draco18s no longer trusts SE Aug 19 '19 at 4:03

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);
  • \$\begingroup\$ bad luck, it starts correctly (with a 90° offset too, as in my formula) but then rotates the sprite image in a direction perpendicular to the Z plane, so it becomes invisible... i guess im obliged to use "angleaxis" to keep the alignement and only turn around the Z axis. when i look back to the formula, it seems mathematically correct (i dont explain the 90° offset tho). it seems to me that the issue is that mathf use float variables -only 6 decimals, i guess i should use custom vector3 functions with double variable precision to enhance the result...headache ahead ^^ \$\endgroup\$ – Denis Taquet Jun 15 '16 at 10:20
  • \$\begingroup\$ and as quaternions use float too, i will always get some limitation. i did all the preliminary math with System.Math instead and converted to a float in the end, its a little better but not perfect at all. i think ill have to hide this by adding some randomness in the shots... \$\endgroup\$ – Denis Taquet Jun 15 '16 at 10:40
  • \$\begingroup\$ What you're seeing definitely doesn't look like a float precision issue. 32 bit floats are more than accurate enough for this task. Could there be a mismatch in how your graphic is oriented relative to its local axes? \$\endgroup\$ – DMGregory Jul 14 '16 at 12:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.