Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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 /
  • Help Room /
avatar image
0
Question by Henerz15 · Jun 21, 2016 at 10:02 PM · rotationquaternionoffsetaimturrets

How to fix turret rotation offest by 90 degrees

So I have already tried code from lots of different tutorials and code from answers from people with similar problems but none of it seems to work. I've imported the turret multiple times each with a different starting rotation which is what most solutions seemed to say. But i can't seem to find how to fix the rotation.

Each time i play the game the turrets rotate to face the target, but instead of the barrel of the gun pointing at the target, it faces it with the side of the turret so its 90 degrees off.

How can i adjust the angle to correct the aim?

 public class TurretAimScript : MonoBehaviour
 {
 
     public Transform target;
     public float smoothing = 10.0f;
     Quaternion lookAtRotation;
 
     void Update()
     {
         lookAtRotation = Quaternion.LookRotation(target.transform.position - transform.position);
 
         if (transform.rotation != lookAtRotation)
         {
             transform.rotation = Quaternion.RotateTowards(transform.rotation, lookAtRotation, Time.deltaTime * smoothing);
         }
     }
 }

alt text

turretbrokenaim.jpg (73.1 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
1

Answer by Dibbie · Jun 22, 2016 at 04:50 AM

2 ways.

The first is, when your importing it, and you can even do this directly inside of Unity, look at the hierarchy's transform gizmo, and the blue arrow is Z, which is its forward... Whichever way that is facing, is what the turret considers its "Forward" to be, so if its facing its side... Its side is considered its "forward"

So if you unparent the hierarchy, and manually rotate it in Unity, then parent the hierarchy structure back, itll be facing the correct way - dont rotate the object while it is still in the parent, or itll just kind of mess up and not look right.

Alternatively, you could do it by code as well, after looking at your target, do something like:

 Quaternion turretRot = transform.localRotation; //temp variable to store the current rotation
 
 turretRot.y -= 90; //subtract 90 degrees from its looking angle
 
 transform.localRotation = turretRot; //apply the updated rotaton

Which is all untested code, but seems like the idea your looking for.

Comment
Add comment · Show 2 · 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 Henerz15 · Jun 22, 2016 at 09:57 AM 0
Share

Thanks for your response. Unfortunately even un-parented and given a new rotation it didn't work. I tried using an empty game object, offsetting the rotation in that and then parenting the turret to that but it also just ignored that and rotated the same.

The code also didn't work as turretRot.y is a Quaternion and it didn't seem to like taking 90 from it. However it gave me an idea of how to fix it. I changed the euler angles of the y value ins$$anonymous$$d which worked!.

I thought I'd share my code to show how i got it working and hopefully it might help somebody else.

 public class TurretAimScript : $$anonymous$$onoBehaviour
 {
 
     public Transform target;
     public float smoothing = 10.0f;
     Quaternion lookAtRotation;
     public float offset = 5.0f;
     Vector3 tmp;
 
     void Update()
     {
         lookAtRotation = Quaternion.LookRotation(target.transform.position - transform.position);
 
         if (transform.rotation != lookAtRotation)
         {
             tmp = lookAtRotation.eulerAngles;
             tmp.y -= 90;                            // fix the rotation
             tmp.z = (tmp.x * -1) - offset;          // uses the x value to aim vertically and iff offset so it aims at the center of the target
             tmp.x = 0;                              // stops the turret from rotating in the wrong axis
 
             Quaternion newRot = Quaternion.Euler(tmp);  
 
             transform.rotation = Quaternion.RotateTowards(transform.rotation, newRot, Time.deltaTime * smoothing * 2);
         }
     }
 }

avatar image Dibbie Henerz15 · Jun 22, 2016 at 03:44 PM 0
Share

Glad I could help - I completely forgot about EulerAngles as a solution, and also forgot to say - 90f, as a Quaternion only accepts float values and not integers, haha.

avatar image
0

Answer by dahiyabunty1 · Aug 08, 2021 at 04:50 PM

if someone face this problem again .

solution add second parameter in function lookrotation vector 3.up or anything try change fit ur need

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

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

Related Questions

Using quaternions to align game objects. 1 Answer

how can I change the interpolation type of animationclip's rotation property 1 Answer

3 Axis Camera Rotation 0 Answers

Help with creating rotational mapping 0 Answers

Know when the object is rotated? 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