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 ronronmx · Apr 26, 2012 at 05:52 PM · rotationquaternioneulerangles

Writing a custom LookAt() function

I need to use the "LookAt()" function to rotate an object towards a waypoint, but since I am always moving my object in the X axis using Vector3.right, I need a version of LookAt() which will point the object towards the target on the X axis instead of the Z axis.

Can anyone tell me how the LookAt() function works internally, so that I can write my own version using a different axis?

Thx for your time! Stephane

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

Answer by Zergling103 · Apr 27, 2012 at 12:57 AM

You don't need to write your own code to achieve the effect you are looking for.

I'm not sure which way to interpret what you're asking, but I'll my answer for each:

1) You're trying to rotate the object so that it doesn't pivot around the Y axis.

 transform.LookAt(target, newAxis); // Change "newAxis" to the axis you want.

2) You want to make it so that it is not necessarily the forward axis of an object that looks at another object. You might want the side to be angled towards the target.

 transform.LookAt(target);
 transform.Rotate(eulerAngleOffset, Space.Self);

eulerAngleOffset is the rotation in local space applied AFTER the transform is rotated to look at the target. For example, to orient left side of the transform towards the target instead of the forward side, eulerAngleOffset would be:

 eulerAngleOffset = new Vector3(90, 0, 0);

Top would be:

 eulerAngleOffset = new Vector3(0, 90, 0);

To roll the camera to the side:

 eulerAngleOffset = new Vector3(0, 0, 90);

These functions can be broken down into operations with Quaternions. When you can wrap your head around them, they are extremely useful. I'm not sure of what the source code is for the quaternion functions, but here is an example of code that "points" an arbitrary local direction of an object towards an arbitrary global direction, taking the shortest way around possible:

 Vector3 fromGlobalDir = transform.rotation * fromLocalDir; // Same as Transform.TransformDirection().
 transform.rotation = Quaternion.FromToRotation(fromGlobalDir, toGlobalDir) * transform.rotation; 

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 ronronmx · Apr 27, 2012 at 06:39 AM 0
Share

Thanks for the help! Basically your second solution is what I was looking for. Thing is I'm working with a vehicle with a rigidbody so I need to use smooth rotations with Lerps, and I'm also trying to make the vehicle rotate depending on the speed, but keep the rotation the same at any speed, just make it rotate slower/faster, kinda like if the vehicle was locked on a spline. I have been using Quaternion.LookRotation() and Quaternion.Slerp() ins$$anonymous$$d of LookAt(), which works fine, but I cannot get the rotation to finish at exactly the same vector regardless of speed. Btw, thanks a lot for your examples above, they cleared up a lot of things :)

avatar image nasoukikos · Sep 27, 2014 at 05:09 AM 0
Share

DUDE I LOVE YOU! I was busting my head with this all night! Thank you very much!

avatar image
0

Answer by marcin-majkowski · Jan 29, 2018 at 10:38 PM

@Zergling103 could You please tell me one thing? You post that it is possible to use LookAt with Rotate, i'm trying to do that with this:

 void Fly(){
  transform.LookAt(transform.position + myRigidBody.velocity);
  transform.Rotate(0, 0, speed* Time.deltaTime, Space.Self);
 }

and Rotate() do not work... only when i comment "transoform.LookAt()" line

Do You have any idea why?

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Keeping track of vehicle rotation? 1 Answer

Alternative to Quaternion.LookRotation() ? 2 Answers

Using Quaternion.LookRotation( ) on y axis only, but keep x axis of transform the same? 2 Answers

Smooth rotation in 90° increments 0 Answers

Quaternion.FromToRotation misunderstanding 2 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