Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
This question was closed Oct 19, 2015 at 03:54 PM by infilrtrator_55 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by infilrtrator_55 · Oct 19, 2015 at 05:33 AM · c#rotationtransformquaternion

Weird rotation when using Slerp!!?

I have a gun turret, and I want it to smoothly rotate on global Y axis to face an near by enemy. I'm using Quaternion.Slerp() for this, here is the code:

                 target = col.transform;
                 direct = target.position - transform.position;
                 direct.y = 0;
                 rot = Quaternion.LookRotation(direct);
                 rot = Quaternion.AngleAxis(rot.y, Vector3.up);
                 transform.rotation = Quaternion.Slerp(transform.rotation, rot, Time.deltaTime * 2.0f);

The problem is even after giving planar coords. Slerp is rotating in all the directions. I'm using a simple low poly model to demonstrate:

alt text

I want the turret to rotate, and face the blue cube so that I can play fire animations and particles. But the turret object is rotating weirdly in all 3 directions like this:

alt text

As you can see the turret rotates to face upwards and not the way I wanted. Locally Z-axis is the up and maybe that is the problem? I still don't get how quaternions work. I came across many similar problems but nothing worked for me. I'm smashing my head against the wall for 2 hours. Please help!!!

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

3 Replies

  • Sort: 
avatar image
0
Best Answer

Answer by infilrtrator_55 · Oct 19, 2015 at 08:59 AM

The problem was that the imported model for 3ds Max has messed up axis. So locally and globally model has different axes for a same direction like Z axis was up locally. This was messing up the rotations. I parented the turret to a empty and applied the script to it. Works flawlessly now.

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
1

Answer by toromano · Oct 19, 2015 at 08:48 AM

Why are you using Quaternion.AngleAxis ?

This should work:

 target = col.transform;
 var direct = target.position - transform.position;
 direct.y = 0;
 var rot = Quaternion.LookRotation(direct);
 //rot = Quaternion.AngleAxis(rot.y, Vector3.up);
 transform.rotation =  Quaternion.Slerp(transform.rotation, rot, Time.deltaTime * 2.0f);

if the axis is not right, you can use a dummy parent

Comment
Add comment · Show 1 · 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 infilrtrator_55 · Oct 19, 2015 at 09:02 AM 0
Share

Hah, I was trying something stupid which got copy pasted. Yes, dummy parent thing worked!

avatar image
1

Answer by hexagonius · Oct 19, 2015 at 08:19 AM

you are correct, that is the problem. if you cannot rotate it any other way, wrap your turret in another gameobject which is setup correctly. LookRotation uses z as forward.

Comment
Add comment · Show 1 · 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 infilrtrator_55 · Oct 19, 2015 at 08:36 AM 0
Share

@hexagonius Thanks for replying. Do you think that make the turret child of a empty object and applying the script to that empty would solve this problem?

Follow this Question

Answers Answers and Comments

35 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

Related Questions

Auto level an object 0 Answers

Flip 3D Character Rotation 180 on Y axis 1 Answer

How can I set rotation properly? 0 Answers

How to rotate a camera slowly along a tween? 1 Answer

Rotation problem 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