- Home /

# Trig for setry gun aiming works but always aims a bit above the target

I made used a little trigonometry to make a sentry gun automatically aim at targets which it works (At least it aims pretty close to the target) but it always appears to make the gun aim above the target by roughly 0.5 - 10 degrees depending on the location of the target.

Note - I will add the interpolation later but any optimizations or code clean up would be handy too.

The code: ` Vector3 v = this.Target.transform.position - this.gameObject.transform.position;`

` `

if ( v.z > 0 ) { this.XAxis.transform.rotation = Quaternion.Euler ( Mathf.Atan ( v.y / v.z ) * Mathf.Rad2Deg * -1, this.XAxis.transform.rotation.eulerAngles.y, this.XAxis.transform.rotation.eulerAngles.z ); this.YAxis.transform.rotation = Quaternion.Euler ( this.YAxis.transform.rotation.eulerAngles.x, Mathf.Atan ( v.x / v.z ) * Mathf.Rad2Deg, this.YAxis.transform.rotation.eulerAngles.z ); }

`if ( v.z < 0 ) { this.XAxis.transform.rotation = Quaternion.Euler ( Mathf.Atan ( v.y / v.z ) `

* Mathf.Rad2Deg, this.XAxis.transform.rotation.eulerAngles.y, this.XAxis.transform.rotation.eulerAngles.z ); this.YAxis.transform.rotation = Quaternion.Euler ( this.YAxis.transform.rotation.eulerAngles.x, Mathf.Atan ( v.x / v.z ) * Mathf.Rad2Deg + 180, this.YAxis.transform.rotation.eulerAngles.z ); }

Explanation of the Variables:

v = the difference in the positions of the gun and target

XAxis = the part of the gun that does the rotation on the X axis (Up and Down)

YAxis = the part of the gun that does the rotation on the Y axis (Left and Right)

*Update - Apparently the issue of the gun aiming too high gets worse when the difference in the z positions of the target and gun get close to zero. Another new issue is that I noticed that the gun also seems to aim below the target if the target is lower than the gun.

**Answer** by Owen-Reynolds
·
Feb 01, 2014 at 08:45 PM

Just a guess, your gun has (000) at the base, a few feet below the barrel? So that 1st line incorrectly computes the aim-angle from the wrong part of the gun?

Replace `transform.position`

(the base) with `XAxis.position`

(swivel point of the barrel) in the first line?

That's the same sort of glitch you get a lot in raycasting, where it's easy to mistakenly look from my feet to the enemy's feet.

Well, that helped part of the issue - your solution fixed the ai$$anonymous$$g at first making the gun aim correctly but the gun's aim eventually started to deviate when the target was moved higher or lower

Looking at the new(?) trig code above (or maybe I just didn't notice it before) could replace nearly all with: `Quaternion aimAngle = Quaternion.LookRotation(targPos-myPos);`

.

If you look at the docs, they hint/say that changing individual eulerAngles, like your code does, can be buggy. They are computed from the quaternion, in a complex relationship.

Apparently, using the quaternion approach has brought up a new question over how to copy only one axis of rotation to each of the two different parts the gun rotates on so that only one part rotates sideways and only one rotates vertically.

Clever use of LookRotation can avoid trig altogether in most cases. But if you need/prefer trig, the final Quaternion rotations can be computed *from* your own xyz-angles just fine. Ex: `base.rotation = Quaternion.Euler(0, ySpin, 0);`

then `barrel.rotation=Quaternion.Euler(xUpTilt, ySpin, 0);`

-- NOTE: order is y,x,z, so xUpTile is on local x, after the spin, which is perfect. Or use localRotation for the barrel. Where `xUpTilt`

uses atan of the xz-hypotenuse and y.

Ai$$anonymous$$g a gun is also a pretty common topic here.

Looks like a little thought overnight and your ideas helped me come up with this solution:

Quaternion q = Quaternion.LookRotation ( this.Target.transform.position - this.BarrelBase.transform.position );

this.XAxis.transform.localRotation = new Quaternion ( -q.x, 0, 0, q.w );

this.YAxis.transform.localRotation = new Quaternion ( 0, 0, q.y, q.w );

It came to me that the only identity between euler and quaternions was 0 and that the swivel points on the gun had local rotations 0 on each axis so I came up with this solution. Although the axes were a little switched up on the YAxis swivel point but after some correction, Debug.DrawRay proved it was ai$$anonymous$$g correctly. So thanks for your help!

### Your answer

### Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

### Follow this Question

### Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

aim at an object that approches 2 Answers

aiming at center of screen 1 Answer

C# Error: Argument out of range 2 Answers

Unexpected LAG :\ 1 Answer