Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by Fabio23000 · Sep 05, 2020 at 04:31 PM · rotationquaternionspherical

Look at mouse on a spherical world

Hello guys. Im trying to make my player look at the mouse position while changing his rotation on the planet surface, but Im having problems. My goal is to combine the rotation on the Y axis (where the player is looking) and the rotation on the X and Z when moving on this sphere. rb stands for rigidbody, as Im using it to push the player towards the center of the planet. This is what I've done so far:

 Vector3 gravityUp = (rb.position - position).normalized;
 Vector3 localUp = rb.transform.up;
         
 rb.AddForce(gravityUp * planet.gravity);
 
 Ray camRay = Camera.main.ScreenPointToRay(Input.mousePosition);
 RaycastHit floorHit;
         
 if (Physics.Raycast(camRay, out floorHit, floorMask)) {
         Vector3 playerToMouse = floorHit.point - transform.position;
         transform.rotation = Quaternion.LookRotation(playerToMouse, gravityUp);
 }
 Quaternion rotation = Quaternion.FromToRotation(localUp, gravityUp) * rb.rotation;
 rb.MoveRotation(rotation);

I changed a lot of things but none of them worked, except this one but the player starts to shake really hard and it leans forward when I have the mouse cursor near it. Any help would be really appreciated.

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
0

Answer by streeetwalker · Sep 05, 2020 at 07:49 PM

Hi @Fabio23000, interesting problem, but it's not clear in your description where your LookAt point actually is, and how you want the character to behave.

LookAt rotates an object such that the forward z axis points at the LookAt point. But I gather this is not what you want, because that will also pitch (tilt) the character on the X axis

First where is the LookAt point? In your code seems like you are projecting the mousePosition onto the planet.

From there I think you want to rotate the player on Y so that Z points in the direction of that projection without actually looking at it...

If so, I think trying to use LookAt will be more complex than finding the angular difference between the rotation of the player and the projection point, and use that to rotate the player.

Google "spherical triangles" to find the maths behind what I think you will need to do. I think that the player position and facing will define one side of the triangle

For example, define a spherical triangle with corners A, B, and C. Say the player is at corner A. Let C be your projection point. So AC defines the side of the triangle you want to rotate toward.

Let the segment AB define the side of the triangle the player is facing. Corner B is unknown, but you can chose any arbitrary point on the line defined by the direction the player is rotated - because all we are interested in is the angle between the sides. That said, of the calculations you will need to perform corner B is probably conceptually the most difficult to come up with, math-wise

Once you find the angle, it will be easy to create the quaternion to apply to your player's rigidbody.

All that is off the top of my head, but I think it will be the easiest way to do what you want. I'm sure there are other ways to do it, and perhaps there is another simpler solution someone else can come up with.

Comment
Add comment · Show 7 · 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 Fabio23000 · Sep 05, 2020 at 10:25 PM 0
Share

Thanks for your answer. This is what the script above does https://imgur.com/a/zvdURPj As you can see it kinda works but I can't "merge" the 2 different rotation, so it goes from one to the other and starts to shake. I hope this clarifies better what I'm trying to achieve.

avatar image streeetwalker Fabio23000 · Sep 06, 2020 at 06:27 AM 0
Share

Understood. Well no, your code does not really implement the algorithm I outlined. It approaches some elements of what the algorithm achieves. I think In essence you are trying to apply the rotation calculated from LookRotation and then use the result to compound the final rotation. One problem is the LookRotation is not actually applied to the rigidbody:

So, You have a conflict in trying to rotate the player's rigidbody and at the same time apply a rotation to the player's transform. If you are dealing with a rigidbody, you must use the physics engine API calls to perform all transformations.

The rigidbody component controls the transform of the object - not the other way around.

If you apply a rotation to the player's transform, the physics engine doesn't know about that. On the next physics engine update (just before the FixedUpdate message is sent) the physics engine will wipe out your transform change. You apply a rotation to the transform, but that does not rotate the rigidbody. That is why you get a jittering behavior.

(You also need to ensure all of the code that manipulates the rigidbody are in FixedUpdate - i'm sure you know that, but your code doesn't display it, so just to be sure.).

However, I'm not sure that is at the core of your problem. I'd have to think about it more. Perhaps your code will work if, instead of trying to apply a rotation to the player transform in the raycast condition block, you calculate the rotation and store it in a quaternion and then use that place of multiplying by rb.rotation in the line of code after your raycast in .

That said, I believe the algorithm outlined will give you a prefect solution.

avatar image Fabio23000 streeetwalker · Sep 06, 2020 at 04:27 PM 0
Share

I really appreciate your answer and explanation, but I can't seem to fully understand what you mean. Would you $$anonymous$$d writing a pseudocode? This quaternion/rotation thing is melting my brain

Show more comments
avatar image
0

Answer by Rostes · May 17, 2021 at 11:55 PM

I am in the same situation except that I want to move my character to a Vector3 position generated by a raycast. My character rotates badly and spins like on the image shared by Fabio23000. https://imgur.com/a/JnkaLsi

If anyone has a solution, thank you very much!

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

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

How do i rotate an object 90 or -90 degrees upon collision? 3 Answers

How to set y rotation of an object to be z rotation of another? 1 Answer

How to use Quaternion.Slerp with transform.LookAt? 3 Answers

How can I make my game object move like this? 0 Answers

how to rotate bullet towards enemydirection? 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