• 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 Azuri · Feb 24, 2014 at 01:40 AM · lookatalignspherical

character lookAt on spherical planet - Face Y to Target and XZ to Planet !?

Hi, I'm trying to solve this but unfortunately, can't find a good solution for this.

My character is standing on a spherical Planet and should always look into the direction of another object, which is moving around on a planet. This means, his Y rotation should rotate him into the direction of the object and the X and Z rotation should rotate him into the direction of the planet.

What is the best way to do this?

TIA

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
4

Answer by robertbu · Feb 24, 2014 at 01:50 AM

I'm going to assume that you already have code that sets the character's 'y' to align with the planet, and you are only looking for the code to make the character face the correct direction. Given that the characters orientation is arbitrary (i.e. he can be rotated to any angle), the solution is to project the the position to look at onto a mathematical plane that aligns with the character's 'y' and passes through the pivot point of the character. 'target' in the script below is the object the character is to look at. Here is a bit of untested code that should do the job:

 #pragma strict
 var target : Transform;
 
 function Update() {
     var pos = ProjectPointOnPlane(transform.up, transform.position, target.position);
     transform.LookAt(pos, transform.up);
 }
 
 function ProjectPointOnPlane(planeNormal : Vector3 , planePoint : Vector3 , point : Vector3 ) : Vector3 {
     planeNormal.Normalize();
     var distance = -Vector3.Dot(planeNormal.normalized, (point - planePoint));
     return point + planeNormal * distance;
 }
Comment
Add comment · Show 5 · 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 Azuri · Feb 24, 2014 at 02:08 AM 0
Share

Works like a charm. Thank you a lot!

avatar image Azuri · Feb 25, 2014 at 01:50 AM 0
Share

Do you also have an advice to lerp the Y rotation to get a smooth rotation? I'm using transform.rotation = Quaternion.FromToRotation (Vector3.up, dir2Planet); to align the player to the planet. Unfortunately, the Y Rotation is always completely off after that, which stops me from simply lerping it.

avatar image robertbu · Feb 25, 2014 at 02:49 AM 1
Share

I'll assume that 'planet' is the transform of the planet. You could do:

 transform.rotation = Quaternion.FromToRotation(transform.up, transform.position - planet.position) * transform.rotation;

If you want to Slerp it, you can do:

 var rotation = Quaternion.FromToRotation(transform.up, transform.position - planet.position) * transform.rotation;
 transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * speed);
avatar image Azuri · Feb 26, 2014 at 04:41 PM 0
Share

Thank you again! :D

avatar image sillanstudios · Oct 11, 2015 at 04:06 PM 0
Share

$$anonymous$$an, you're an absolute genious. I've been looking for a solution to the problem for over 2 weeks now and stumbled across your answer. Works perfectly. You're the best, I really appreciate you sharing the information :D.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

20 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

Related Questions

changing transform of an object (without using "look at") 2 Answers

How to move a planar object around a sphere? 2 Answers

Align transform Y axis to -Physics.gravity 1 Answer

Shooting bullet towards mouseposition 2D. 2 Answers

Simple LookAt Rotation with offset pivot 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