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 /
  • Help Room /
avatar image
1
Question by fighder · Jan 24, 2016 at 06:11 AM · cameraphysicsquaterniongravitylookrotation

Changing gravity direction of player, and view.

I tried doing this thinking it shouldn't be too hard, but man am I wrong. Or maybe I am just bad.

First problem, I can't use any rigidbody base rotation (add torque, moverotation, rotation, etc), reason being I am using a capsule collider for my player, and physics is making it fall to its sides, and when colliding with walls, it just makes my player spin slowly for some reason, so I froze the rotation.

The changing gravity direction part is not the problem, but the problem is rotating SMOOTHLY to the corresponding direction. Basically my player can select any wall, ceiling, or floor they point at, and make that the ground floor for them, causing a change in gravity direction, then the player rotates where their bottom is touching the new ground floor.

I am not exactly sure what to do with this rotation. I know I need to use Quaternion.LookRotation, and I know the up direction is the normal of the new floor, but I don't know what forward direction it should be. Note that the player can be looking at a wall on an angle, then click on it, and the rotation happen, so the player's forward at the moment of click is not the same as vector3.forward, which is the most trouble some part.

I am just asking for a solution in general, not necessarily improving on top of the one I am doing, so any help will be much 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
1

Answer by Eno-Khaon · Jan 24, 2016 at 08:46 AM

If you consider the way your character will likely be oriented when settled into a correct rotation at any given time, they will likely be standing upright, possibly leaning forward or backward. This means that your character's left and right will generally be guaranteed to be accurate or close enough.

But, your character's not already perfectly-enough aligned? Well, that's what axis-correction is good for.

First, take your character's current forward direction and up relative to the current gravity and take their cross product as your new right vector relative to the ground.

 // C#
 Vector3 newRight = Vector3.Cross(-gravity, transform.forward);

Now that you have a ground-aligned right vector, it's time to reconstruct your forward vector from it as your next baseline. This takes a cross product again:

 Vector3 newForward = Vector3.Cross(newRight, -gravity);

With those, you now have your baseline orientation before applying any orientation modifiers (such as having your character lean into a turn, or looking around or up and down).

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
avatar image
0

Answer by NGC6543 · Jan 24, 2016 at 08:47 AM

First of all you should use Quaternion.Lerp or Quaternion.LookRotation to make it being smoothly interpolated. And use transform.forward instead of Vector3.forward. transform.forward uses your player GameObject's local forward direction.

And the stability issue you asked... you can stabilize the GameObject in the LateUpdate() phase.

     Void LateUpdate(){
         transform.rotation = Quaternion.identity;
     }

This code uprights a GameObject no matter how much torque is applied. But your case the GameObject does change their rotation, so you have to do some work to fit your need.

Hope this helps.

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

51 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

Related Questions

Changing gravity depending on character/camera rotation 0 Answers

CineMachine Freelook - X axis breaks when WorldUpOverride is sideways, any way to fix? 0 Answers

How to make physics for the string of a rising balloon 1 Answer

Smooth Walking Inside a Sphere 0 Answers

Adding Gravity to a game object to make a black hole sucking effect. 1 Answer


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