Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 danielbarta · Aug 14, 2020 at 10:30 AM · rotationrigidbodyrigidbody2d

Limit rotation on dynamic rigidbody

Hi! I have a player character which uses dynamic rigidbody. I want to use the built-in physics, but with a few exceptions. I want to constraint rotation and fall velocity, also speed and rotation itself.

I move and rotate my character using forces in FixedUpdate, and apply the constraints on LateUpdate.

 void FixedUpdate()
     {
         if (!isLocalPlayer)
         {
             return;
         }
 
         Move();
         Rotate();
     }
 
     private void LateUpdate()
     {
         if (!isLocalPlayer)
         {
             return;
         }
 
         ConstraintFallVelocity();
         ConstraintRotation();
         ConstraintRotationVelocity();
         ConstraintSpeed();
     }
 
     void Move()
     {
 
         if ((Input.GetKey(KeyCode.Space) || Input.GetMouseButton(0)) && !_spacePressed)
         {
             CmdShoot();
             Vector2 velocityNorm = _rigidbody.velocity.normalized;
             Vector2 velocityNormMirrored = new Vector2(velocityNorm.x, Mathf.Abs(velocityNorm.y));
             if (velocityNormMirrored.y == 0f)
             {
                 velocityNormMirrored.y = 1;
             }
             velocityNormMirrored.x = velocityNormMirrored.x * -1;
             _rigidbody.AddForce(VectorHelpers.Rotate(VectorHelpers.DegreeToVector2(_rigidbody.rotation), 90).normalized * _force);
             _spacePressed = true;
         }
 
         if ((!Input.GetKey(KeyCode.Space) && !Input.GetMouseButton(0)) && _spacePressed)
         {
             _spacePressed = false;
         }
     }
 
     void Rotate()
     {
         float input;
         if (Mathf.Abs(Input.acceleration.x) > Mathf.Abs(Input.GetAxis("Horizontal")))
         {
             input = Input.acceleration.x;
         } else
         {
             input = Input.GetAxis("Horizontal");
         }
         _rigidbody.AddTorque(input * _rotationSpeed * -1);
     }
 
     void ConstraintFallVelocity()
     {
         if (_rigidbody.velocity.y < _maxFallVelocity * -1)
         {
             _rigidbody.velocity = _maxFallVelocity * _rigidbody.velocity.normalized;
         }
     }
 
     void ConstraintRotation()
     {
         float rotationX = transform.eulerAngles.z;
         Vector3 euler = transform.eulerAngles;
 
         bool clamped = false;
         // euler.z is between 0 and 360. For example, if it is rotated left it is 10, if rotated right, it is 350
         if (euler.z > 180 && 360 - euler.z > _maxRotation)
         {
             clamped = true;
             euler.z = 360 - _maxRotation;
         } else if (euler.z < 180 && euler.z > _maxRotation)
         {
             clamped = true;
             euler.z = _maxRotation;
         }
 
         if (clamped)
         {
             _rigidbody.angularVelocity = 0;
             transform.eulerAngles = euler;
         }
     }
 
     void ConstraintRotationVelocity()
     {
         _rigidbody.angularVelocity = Mathf.Clamp(_rigidbody.angularVelocity, -_maxAngularVelocity, _maxAngularVelocity);
     }
 
     void ConstraintSpeed()
     {
         if (Mathf.Abs(_rigidbody.velocity.x) > _maxSpeed || Mathf.Abs(_rigidbody.velocity.y) > _maxSpeed)
         {
             // clamp velocity:
             Vector3 newVelocity = _rigidbody.velocity.normalized;
             newVelocity *= _maxSpeed;
             _rigidbody.velocity = newVelocity;
         }
     }

When I rotate, and the max rotation is reached, I experience a small jump in character movement. I think it is because though I took care of rotation limit, the new position has already been calculated.

Is there a right way to do this using dynamic rigidbody?

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

0 Replies

· Add your reply
  • Sort: 

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

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

rigidbody.rotation locally; Help! 1 Answer

2d keep player upright 1 Answer

Rotating rigidbody problem 0 Answers

Question About NavMesh Agent and rotation 1 Answer

[VR] Rigidbody.AddForce() following controllers rotation 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