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 alexman · Mar 04, 2014 at 01:41 AM · rotationquaternionlook at

Basic quaternion question

Hi,

I am new to Unity and I am still learning how to work with quaternion...

There are 3 objects in my scene at the moment, a bear, a box, and a sphere. The bear is current looking at the box, and I want him to smoothly turn to look at the sphere.

I can make him face the sphere right away using lookAt : bear.transform.lookAt(sphere.transform);

However I would like to make him turn smoothly, so I was looking to use Quaternion.Lerp, I believe that I will need to do something like this in the void Update() : bear.transform.roation = Quaternion.Lerp(bear.transform.rotation, newQuaternion, time.deltaTime);

Now the question is, I don't know how to calculate the newQuaternion, the quaternion for him to look at the sphere.

I tried to get the bear to transform.lookAt the sphere and then copy the rotation, and then make him look at the box again, and then use Quaternion.lerp, which works but i believe this is not the right way, any suggestion?

Thank you.

Regards, Alex

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

Answer by danielskovli · Mar 04, 2014 at 03:53 AM

Hopefully I've understood correctly, would something like this work?

 public GameObject target;
 public float speed = 45f;
 
 // Update is called once per frame
 void Update () {
     Vector3 direction = target.transform.position - transform.position;
     Quaternion rotation = Quaternion.LookRotation(direction);
     transform.rotation = Quaternion.RotateTowards(transform.rotation, rotation, speed * Time.deltaTime);
 }

Stick that on your bear object, and drag the sphere into the target reference. The bear will smoothly turn to look at the sphere, and keep following it forever (until you code otherwise).

Keep in mind though that this only deals with the forward-Z axis, so your bear must be modeled accordingly. I'm sure you're aware of this already though.

Cheers, Daniel

Comment
Add comment · Show 4 · 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 alexman · Mar 04, 2014 at 04:48 AM 0
Share

Thanks, it works. Anyway to limit it to rotate only on Y axis?

avatar image danielskovli · Mar 04, 2014 at 04:58 AM 0
Share

Try zeroing out the direction's Y value by adding this directly under Vector3 direction: direction.y = 0;

avatar image alexman · Mar 04, 2014 at 05:00 AM 1
Share

Perfect, thank you very much $$anonymous$$ !

avatar image danielskovli · Mar 04, 2014 at 05:01 AM 0
Share

No worries :)

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

Problem finding relative rotation from one quaternion to another 4 Answers

How can I get the rotation between two objects 1 Answer

Rotate object following mouse movement. Object jumps/ flips 1 Answer

Projectile Rotation To Match Direction Shot? Mobile Joystick 2 Answers

How to make a plane face a static but rotating camera? 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