Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Mushu · Mar 27, 2014 at 01:33 PM · rotationlookatoffsetturretpivot

Simple LookAt Rotation with offset pivot

Hi there

This should be simple and common enough but I can't for the life of me figure out how to do this.

I have a turret that needs to "LookAt" a target but it also has an offset pivot, so using Quaternion.LookAt( target.position ); makes the pivot look at the target resulting in the turret aiming slightly above the target.

What I need is a way of achieving the aiming illustrated in bottom drawing of my expertly drawn picture.

alt text

untitled-1.png (24.9 kB)
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by robertbu · Mar 27, 2014 at 08:22 PM

Let me start with a quick hack. Let's assume that your target is a transform and that the dogleg measures 'offset' in length. You an do:

 transform.LookAt(target);
 pos = target.position;
 pos -= transform.up * offset;
 transform.LookAt(pos);

This hack points the pivot point at the target, then it moves the target 'down' by the amount of the offset then aims again. This is a simple, but not perfect solution given the geometry. It should be very close if there is any significant distance between the gun and the target.

Here is a second solution that has the potential to be more accurate. It is untested, and I'm not 100% certain I got it right. It calculate the angle difference and rotates the gun around the vector3.right. I believe it will have problems if the gun has any local 'z' rotation.

 transform.LookAt(target);
 var dist = (target.position - transform.position).magnitude;
 var angle = Mathf(offset/dist) * Mathf.Rad2Deg;
 transform.rotation = Quaternion.AngleAxis(-angle, transform.right) * transform.rotation;
Comment
Add comment · 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
0

Answer by CiberX15 · May 12, 2018 at 07:01 PM

For what it's worth I needed a variation of this that gave me the total angle without using LookAt. This of course would not work in 3D but it was pretty effective for my needs. It also comes with the option of moving the center of the object as well, so if you had a turret image where the pivot was far right and the muzzle was far left it could handle it. Left the debug lines in so you can see how it is lining up. Thanks to aldonaletto here https://answers.unity.com/questions/181867/is-there-way-to-find-a-negative-angle.html for the get angle logic.

Code:

 public static float GetAngle(Vector3 A, Vector3 B)
     {
         //Find angle and ensure we can get a negitive angle if needed
         float angle = Vector3.Angle(A, B);
         Vector3 cross = Vector3.Cross(A, B);
         if (cross.z < 0)
         {
             angle = -angle;
         }
 
         return angle;
     }
 
     /// <summary>
     /// Rotates the selected transform based on its pivot point to aim at the desired point
     /// </summary>
     /// <param name="transform"></param>
     /// <param name="point"></param>
     public static void AimAt(Transform transform, Vector3 pivot, Vector3 point, Vector3 center)
     {
         //Find pivot and center relative to transform in world space (making sure to apply rotation to pivot offset)
         Vector3 piv = transform.position + (transform.rotation * pivot);
         Vector3 cent = transform.position + (transform.rotation * center);
 
         //Find vector pointing to target
         Vector3 targetAim = point - cent;
 
         float angle = GetAngle(transform.up, targetAim);
         transform.RotateAround(piv, Vector3.forward, angle);
 
         Debug.DrawRay(piv, Vector3.up * 0.5f, Color.red);
         Debug.DrawRay(piv, Vector3.right * 0.5f, Color.red);

         Debug.DrawRay(cent, targetAim, Color.green);
         Debug.DrawRay(point, Vector3.up, Color.yellow);
         Debug.DrawRay(point, Vector3.right, Color.yellow);
     }
Comment
Add comment · 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

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

21 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 avatar image avatar image

Related Questions

smooth look at with offset 0 Answers

Turret AI script off rotation 1 Answer

Turret Rotation Problems 1 Answer

Vector3.Angle making my object oscilate... 2 Answers

Rotate Turret to Tag 1 Answer


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