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

  • 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? – Dunk Jul 14 '16 at 23:01
  • 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! – Denis Taquet Jul 23 '16 at 0:31

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);
  • 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 ^^ – Denis Taquet Jun 15 '16 at 10:20
  • 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... – Denis Taquet Jun 15 '16 at 10:40
  • 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? – DMGregory Jul 14 '16 at 12:35

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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