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
3
Question by guitarxe · Dec 20, 2014 at 12:34 AM · transformrotate

How to rotate an object so that y axis faces towards another object?

Can someone help me understand what is the math required to rotate one object so that it's UP vector (the local y-axis) points towards another object?

I know that Transform.LookAt does that, but for the FORWARD vector (the local z-axix), but I want to do the same for the y-axis instead, with some slight modification (dampening how fast it can rotate)

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

Answer by EvilTak · Dec 20, 2014 at 04:32 AM

Looking at your comment on the other answer, you should use Quaternion.FromToRotation. And after that, use Quaternion.Lerp. Something like this (This code should be in FixedUpdate for smooth results):

 Vector3 direction = otherObject.position - transform.position;
 Quaternion toRotation = Quaternion.FromToRotation(transform.up, direction);
 transform.rotation = Quaternion.Lerp(transform.rotation, toRotation, speed * Time.time);

Here is the result (A cylinder poinsting towards the main camera with speed = 0.1f): alt text

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 guitarxe · Dec 20, 2014 at 07:25 PM 0
Share

Thanks, this worked awesomely! Here's the result :) http://gfycat.com/WarpedBlueItalianbrownbear

avatar image EvilTak · Dec 21, 2014 at 02:53 AM 0
Share

Actually, you can change Vector3.up to transform.up in the 2nd line.

avatar image Nischo · Jul 11, 2015 at 01:01 PM 0
Share

You said "should be in FixedUpdate for smooth results" but wouldn't it be better if you place it in update and use Time.deltaTime ins$$anonymous$$d? Can't get smoother than that, or am i missing something here?

avatar image EvilTak · Jul 11, 2015 at 01:55 PM 0
Share

I should rephrase the sentence. It'll make the movement more accurate, no matter what the FPS of the game'll be. Plus, I$$anonymous$$O it's better to do computations in FixedUpdate.

avatar image
1

Answer by Kartik1607 · Dec 20, 2014 at 02:59 AM

Hey.

Lets try some coding.

First, we create a public Transform variable to store the target object. (to whom we want to look)

 public Transform target;

Now, we want to look at the target. So, we use LookAt function.

     void Update () {
         transform.LookAt (target.position);
     }

This produces this effect. Tree looking at cube

But, we want the Y axis to point to cube.

If we observe, we can see that Y axis is 90 degree to the direction tree is looking at. So, we simply rotate the tree by 90 degree around X axis.

     void Update () {
         transform.LookAt (target.position);
         transform.Rotate (new Vector3 (1.0f, 0, 0), 90);
     }

This makes Y axis point towards the object.

Tree looking at cube correctly.

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 guitarxe · Dec 20, 2014 at 04:02 AM 0
Share

Hm, haha, yeah that works.

However, you see, my intention was to understand how LookAt works so that I can apply it myself in code, because I want it to rotate at a slow pace and not instantly.

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

27 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

Related Questions

How to prevent Z axis rotation ? 1 Answer

My object isn't rotating to the transform of another object. How do I fix this? 1 Answer

Rotating a bone via scripts 1 Answer

making one object a parent of another via javascript 2 Answers

Change Transform through script ... while animation playing? Animator Controller with Mixamo Characters 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