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 digzelot · May 28, 2014 at 09:46 PM · quaternionlock

How to lock a quaternion axis?

Hello, I have this block of code that tracks my player

 transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(target.position - transform.position), turnSpeed * Time.deltaTime);
         if(Vector3.Distance(transform.position, target.position) < 50)
         {
             transform.position += transform.up * 0.2f * Time.deltaTime;
             transform.position += transform.forward * moveSpeed * Time.deltaTime;
             transform.position = new Vector3(transform.position.x, transform.position.y, 0);
         }

but I have a 2d sprite that its attached to - I need to lock the (z?) axis on the rotation, but when I try to edit the quaternion, the character stops tracking the player

any ideas?

Comment
Add comment · Show 3
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 digzelot · May 29, 2014 at 01:34 AM 0
Share

bump, still haven't found a solution to this... surprised at how fast these forums fill up! Anyway, thanks for any advice

avatar image supernat · May 29, 2014 at 05:00 AM 0
Share

Quaternions are represented by an axis vector and angle about that axis, so what do you mean by locking the Z axis, and how are you trying to go about that? Some context will help, why do you need to lock the Z axis, not really enough to go on here. If I had to guess, you may want to do something like this:

 Vector3 euler = transform.rotation.eulerAngles;
  
 euler.z = 0;
  
 transform.rotation = Quaternion.Euler(euler);
  

Of course, that's the long way. You could also just set transform.eulerAngles directly ins$$anonymous$$d.

avatar image digzelot · May 30, 2014 at 05:16 PM 0
Share

Thanks - I have a section of my game that is a side scroller (3d environment and characters), and there are enemies that are made from quads (sprite sheet animations). The problem is that when I Instantiate the enemies, since they are quads, they rotate along an axis that makes them perpendicular to the camera, like looking at a piece of paper from the side - I need them to not rotate along an axis so they are always facing flat to the camera. I'll try what you've posted!

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by robertbu · May 31, 2014 at 02:33 AM

The issue here is not how to lock an axis. The issue is that Quaternion.LookRotation() is the wrong way to rotate a sprite in a 2D setting. LookRotation() faces the positive 'z' side of an object in a specified direction. For Sprites and Quads, you typically want to face either the right side, or the top side of the object towards another object. Atan2() is on solution. For the right side being forward, you could do:

 var dir = target.position - transform.position;
 var angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
 transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.AngleAxis(angle, Vector3.forward), turnSpeed * Time.deltaTime);

If currently the top is considered forward, you can either rotate the sprite in Photoshop, or you can add 90 degrees to the angle before passing it to AngleAxis().

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

22 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

Related Questions

Quaternion LookRotation/Slerp Axis Lock 3 Answers

Gimbal lock - object moving on surface of sphere 0 Answers

Rotating an object around Z axis using Lerp 2 Answers

Creating a multiple part turret what locks onto certain axis. 4 Answers

How to fix gimbal lock? 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