Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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 /
avatar image
1
Question by syaoranoni54326 · Nov 22, 2015 at 01:26 PM · physicscharacter movementaxesaddtorqueupright

Keeping Character Upright with Addtorque

I'm working on making a character controller that is moved solely through Unity's physics engine with no artificial translation or rotation. The character is capsule shaped for ease of use, so it can rotate on the x and z axes without clipping into the floor. I am using AddTorque to keep the character upright so that the forces such as momentum and friction don't cause it to topple so that it cannot get up. This works great when the player is facing forwards (along the global z axis) or backwards. Rotation is corrected gradually so that the character stays upright. However, when the character faces sideways or even diagonally, its rotation appears to be amplified so that it begins to spin and topple (like a spinning coin), and this can only be neutralised by facing forward again, at which point it begins to level out.

 #pragma strict
 
 var walkSpeed = 10;
 var rb : Rigidbody;
 var uprightTorque = 5;
 var right : Vector3;
 var left : Vector3;
 var forward : Vector3;
 var backward : Vector3;
 //yDefault is a gameObject that mimics the rotation of the character on the y axis but no other.
 var yDefault : Transform;
 
 function Start () {
     rb = GetComponent.<Rigidbody>();
 }
 
 function Update () {
     if(Input.GetKey("up")){
         //We add force on the axes of yDefault because using transform.forward would cause the character to progressively
         //accelerate face first towards the ground when tilted forward. The global axes would cause the character to
         //accelerate forward when up is pressed regardless of where he is facing.
         rb.AddForce(yDefault.transform.forward * walkSpeed);
     }
     if(Input.GetKey("right")){
         rb.AddForce(yDefault.transform.right * walkSpeed);
     }
     if(Input.GetKey("left")){
         rb.AddForce(yDefault.transform.right * -walkSpeed);
     }
     if(Input.GetKey("down")){
         rb.AddForce(yDefault.transform.forward * -walkSpeed);
     }
     //adding torque in the opposite direction of character's rotation on x and z to counterbalance a fall.
     rb.AddTorque(transform.right * -transform.rotation.x * uprightTorque);
     rb.AddTorque(transform.forward * -transform.rotation.z * uprightTorque);
 }

The character also has a force based mouselook from side to side (on the y axis), which appears to work fine. Increasing angular drag just slows down the process described above. I've also changed the force and torque on all the values above and more. Can anyone shed some light on why this is happening?

Comment
Add comment · Show 1
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 Swaggre · Nov 22, 2015 at 03:19 PM 0
Share

Try changing transform.rotation to transform.eulerAngles. EulerAngles are x, y and z rotations in degrees. Rotation is a Quaternion

1 Reply

· Add your reply
  • Sort: 
avatar image
7
Best Answer

Answer by Lukas-Brunner · Nov 22, 2015 at 05:51 PM

There is a section named "Constraints" in the Rigidbody component. There you could freeze the x and z rotation. EDIT: Sorry I overread that you don't want any artificial Rotation. If you don't want the rotation to be forced to zero, you could try to apply the forces in global x and z Direction with Vector3.right and Vector3.forward. Additionally i tried this:

 var rot = Quaternion.FromToRotation(transform.up, Vector3.up);
 rb.AddTorque(new Vector3(rot.x, rot.y, rot.z)*uprightTorque);

With a uprightTorque of 100 this works quite fine although the rotation is a Quaternion.

Comment
Add comment · Show 3 · 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 syaoranoni54326 · Nov 22, 2015 at 06:04 PM 0
Share

Works brilliantly. I suppose because the rb.AddTorque is fed a direct path by the values from "FromToRotation" the 'orbit' effect I was experiencing is counteracted. Can't thank you enough.

avatar image lucariolu3d · Sep 30, 2020 at 02:34 AM 0
Share

also works in 2d,

var rot = Quaternion.FromToRotation(transform.up, Vector3.up); rb.AddTorque(rot.z*uprightTorque);

avatar image Connorses · Jun 16, 2021 at 05:38 PM 0
Share

Thanks man, I was stuck on this for a while. Had no idea the quaternion would work that way.

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

42 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

Related Questions

If angle is greater than 5, do something, if less than -5, do something else [Answered] 2 Answers

F-Zero Game - Collisions/Physics. 1 Answer

How to prevent camera jitter when moving my Rigidbody first person controller 4 Answers

Having trouble turning a Transform movement into a Rigidbody force. Code included 1 Answer

How do I prevent an object from going though the wall? 3 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