Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Doodles · Feb 01, 2014 at 08:26 PM · aiminggun position

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Doodles · Feb 01, 2014 at 09:13 PM 0
Share

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

avatar image Owen-Reynolds · Feb 02, 2014 at 01:32 AM 0
Share

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.

avatar image Doodles · Feb 02, 2014 at 01:44 AM 0
Share

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.

avatar image Owen-Reynolds · Feb 02, 2014 at 03:12 PM 0
Share

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.

avatar image Doodles · Feb 02, 2014 at 04:44 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

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

C# Error: Argument out of range 2 Answers

Unexpected LAG :\ 1 Answer

How to import the object from server to unity 2 Answers

Rotating plane along z axis 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges