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 /
avatar image
0
Question by AlexSharpeDev · May 03, 2020 at 02:57 PM · quaternionlookatsphereup

LookAt whilst traversing a sphere

Hi,

I have a situation where I have an object that is on a sphere. Imagine the red dot is the object, and the green dot is where the object is facing.

alt text

I have code in place to allow the player to click on a point on the sphere and the object will move there. I now want the object to be facing the direction that it is moving. Initially I just went with:

 object.LookAt(vecNextPointInPath)

However, this makes the object tilt in weird ways as it moves. I want to make sure that the top of the object is always facing upwards of the sphere as well as this LookAt. I am able to make it do this, by setting

 transform.up = transform.position

however when I do this in tandem with the LookAt, the LookAt seems to be stomped over and it stays facing the same direction no matter which direction it is moving.

Does anyone have a solution to this? I imagine there is something with maths/quaternions that I've not figured out yet.

Thanks.

diag1.jpg (27.6 kB)
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

Answer by Namey5 · May 04, 2020 at 12:40 PM

You can use vector projection to find a direction perpendicular to one vector in the direction of another (which, in this case when used as the look direction of the object, would align said object with the surface of the sphere).

 //Where 'spherePos' is the centre of the sphere and 'targetPos' is the world-space position of the look target
 Vector3 up = (transform.position - spherePos).normalized;
 //'targetDir' is just the vector of the target position from the sphere - use whatever you already have here, just make sure it's normalized
 Vector3 targetDir = (targetPos - spherePos).normalized;
 
 //Use the dot product to project the two vectors
 Vector3 forward = targetDir - up * Vector3.Dot (targetDir, up);
 
 //Find the relative quaternion and set it as our rotation
 transform.rotation = Quaternion.LookRotation (forward.normalized);
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 AlexSharpeDev · May 05, 2020 at 02:49 PM 0
Share

Thank you! I'd made a couple of modifications (sphere is always at 0,0,0, and I think you missed the up vector in the final line of code, so I now have this:

 Vector3 up = m_transform.position.normalized;
 Vector3 targetDir = nextPosition.normalized;
 Vector3 forward = targetDir - up * Vector3.Dot(targetDir, up);
 m_transform.rotation = Quaternion.LookRotation(forward.normalized, up.normalized);

and that works perfectly!

avatar image Namey5 AlexSharpeDev · May 06, 2020 at 01:11 AM 0
Share

Good point. I was using a capsule for testing, so the up axis didn't make any visual difference. There's also probably no need to normalize the vectors in the quaternion constructor (up is already normalized and even though I did it for forward in the original, I don't believe magnitude makes a difference to quaternions).

avatar image
0

Answer by JonPQ · May 04, 2020 at 07:01 PM

you can use transform.lookat and specify the up vector for which axis to rotate around. your up vector is the vector position of the object minus the center of the sphere's position... https://docs.unity3d.com/ScriptReference/Transform.LookAt.html

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

128 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 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 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 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

Problems with an object rotating a sphere surface (Quaternions and LookAt) 0 Answers

How to add a rotation to this? 1 Answer

How to make player look at opponents? 1 Answer

Object pulling to a perpendicular angle when on a slope 0 Answers

How do you check if Quaternion.Slerp has finished 5 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