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 /
avatar image
0
Question by Pkmans · Jan 18, 2021 at 06:15 PM · rigidbodymovement scriptspeed issues

Rigidbody character, normalize diagonal speed without limiting external rb.addforces?

Hi! I know normalizing the diagonal velocity is the popular method of fixing faster diagonal speed, but that also means my character cannot go faster than that even from external forces, for example if an explosion happened, the character wouldn't be able to launch away at a faster speed. I have a rigidbody character in Unity3D that I use rb.addforces to control. To limit it's speed I basically zero out my input if my velocity is greater than the maxSpeed.

I know I can probably increase the maxSpeed in events like explosions and dashes etc but is there a solution around this? Becus changing the maxSpeed for every event in the future might be a hassle.

Here's a snippet of my player movement code (without any normalizing):

   void Move() {
         float x = Input.GetAxisRaw("Horizontal");
         float z = Input.GetAxisRaw("Vertical");
 
         //vel relative to look direction
         float xVel = vel.x;
         float zVel = vel.y;
 
         //no input if exceeding max speed
         if (x > 0 && xVel > maxSpeed) x = 0;
         if (x < 0 && xVel < -maxSpeed) x = 0;
         if (z > 0 && zVel > maxSpeed) z = 0;
         if (z < 0 && zVel < -maxSpeed) z = 0;
 
         // normalize diagonal speed and remove input if sliding
         Vector2 flatVel = new Vector2(rb.velocity.x, rb.velocity.z);
         if (Mathf.Abs(flatVel.magnitude) > maxSpeed || isSliding) {
             x = 0;
             z = 0;
         }
 
         CounterMovement(x, z);
 
         //move forward and sideways
         rb.AddForce(transform.forward * z * moveSpeed);
         rb.AddForce(transform.right * x * moveSpeed);
    
         //change direction of player faster
         if (zVel > 0.2f && z < 0) rb.AddForce(-transform.forward * turnSpeed);   
         if (zVel < -0.2f && z > 0) rb.AddForce(transform.forward * turnSpeed);  
         if (xVel > 0.2f && x < 0) rb.AddForce(-transform.right * turnSpeed);   
         if (xVel < -0.2f && x > 0) rb.AddForce(transform.right * turnSpeed);
 
         // //max speed check
         // Vector2 flatVel = new Vector2(rb.velocity.x, rb.velocity.z);
         // if (flatVel.magnitude > maxSpeed) {
         //     flatVel = flatVel.normalized * maxSpeed;
         //     rb.velocity = new Vector3(flatVel.x, rb.velocity.y, flatVel.y);
         // }
     }



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

160 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

Related Questions

Rigidbody movement script 2 Answers

Rigidbody causes ragdoll character to fly 4 Answers

How to make a rigidbody move in the direction of finger(x,y,z) 1 Answer

Child object not moving with parent 3 Answers

(C#) Movement Rotation Script not Working! 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