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 Tomas · Feb 26, 2012 at 12:05 AM · rotationturret

Turret rotation angle

Let's say I have a turret and a target like the ones shown bellow. How do I calculate rotation angle I need to rotate the turret around y axis for it to start hitting the target and the angle at which it becomes rotated too much and stops hitting the target. Keep in mind that the cannon is not placed in the center of the turret. Turret

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
0
Best Answer

Answer by Blazor Ramone · Feb 26, 2012 at 02:25 AM

Ok typically if the cannon was in line with the pivot point of the turret you could use The Quaternion.LookAt function to determine the desired Rotation. However, with this offset turret you are going to need to add an additional offset angle. The magnitude of this angle is going to depend on both how far way the target is from the pivot point and how far offset the turret is from the pivot(measured along an axis perpendicular to direction the turret is pointing). Anyway if you divide the turret offset by the distance you will get the tangent of the angle you need. You can use this to calculate the additional rotation to add. An example:

 public class LookAt : MonoBehaviour 

{

 public Transform target;
     public float offSetAngle;
     float turretOffset = 2.66f;//distance to barrel along x axis
 
 void Update()
 {
     Vector3 toTarget = target.position - transform.position;
     offSetAngle = Mathf.Atan(turretOffset / toTarget.magnitude) * Mathf.Rad2Deg;
     transform.rotation = Quaternion.LookRotation(toTarget)
             * Quaternion.AngleAxis(-offSetAngle , Vector3.up);
     
 }

}

Comment
Add comment · Show 6 · 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 Blazor Ramone · Feb 26, 2012 at 02:32 AM 0
Share

I'm not sure if this is the best solution to this problem and I'm interested in seeing other solutions if anyone has them.

avatar image Tomas · Feb 26, 2012 at 05:47 PM 0
Share

Thanks for your answer. What you have explained works with a couple of modifications, but I am not marking this as a solution yet because you missed one little detail in my question. Firstly, the fixes I needed to do for your solution to work:

  • I want my turret to rotate only along y axis, so "targetPos.y = transform.position.y".

  • Ins$$anonymous$$d of "Quaternion.AngleAxis(-offSetAngle , Vector3.up)" I used "Quaternion.AngleAxis(+offSetAngle , Vector3.up)".

Now the thing you missed in the question. Your solution presents a way to deter$$anonymous$$e the angle I need to rotate my turret so it would start hitting the target right in the middle, but I wanted to find the $$anonymous$$INI$$anonymous$$U$$anonymous$$ angle I need to rotate my turret for it to start hitting the target (just the side of the target not the center) and the angle at which it becomes rotated too much and stops hitting the target. Let's assume my target is circular like the one in the picture.

avatar image Tomas · Feb 26, 2012 at 06:12 PM 0
Share

Sorry, the second fix was only needed because my turretOffset was negative.

avatar image Blazor Ramone · Feb 26, 2012 at 07:22 PM 0
Share

Assu$$anonymous$$g that the target is or can roughly be approximated as a sphere or circle you can compute two offset angles where you add or subtract the targets radius to the turretOffset.

offsetAngle1 = $$anonymous$$athf.Atan((turretOffset - radius) / toTarget.magnitude) * $$anonymous$$athf.Rad2Deg;

offsetAngle2 = $$anonymous$$athf.Atan((turretOffset + radius)/ toTarget.magnitude) * $$anonymous$$athf.Rad2Deg;

one of these will be $$anonymous$$imum one will be max depending on your orientation.

avatar image Tomas · Feb 26, 2012 at 07:29 PM 0
Share

Thank you for your help :)

Show more comments

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

6 People are following this question.

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

Related Questions

Need Help with random rotatition on game turret 2 Answers

Turret script. Making it aim in heigth only. 1 Answer

3D Turret Rotation 0 Answers

3D nested Turret Prefab Rotation 1 Answer

Bullet reload problem 3 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