• 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 sleepyfunnyfish · Nov 11, 2022 at 05:32 AM · movementrigidbodyvelocityspeedrigidbody.addforce

How to add max speed to 3D rigidbody?

I'm making a racing game but with flying cars (no gravity). Trying to add a maximum speed. Tried everyt$$anonymous$$ng I found online and no solution. Below is what I'm working with, no solution attempted.

  Rigidbody rb;
 
  public float AccelSpeed = 100f;
  public float ReverseSpeed = 50f;
  public float RollSpeed = 50f;
  public float PitchSpeed = 50f;
  public float YawSpeed = 50f;
 
  private Vector3 LocalVelocity;
 
  public float MaxSpeed = 5f;
 
  private PlayerInput playerInput;
  private PlayerInputActions playerInputActions;
 
  void Awake()
  {
      rb = GetComponent<Rigidbody>();
 
      playerInput = GetComponent<PlayerInput>();
 
      playerInputActions = new PlayerInputActions();
      playerInputActions.Player.Enable();
  }
 
  private void FixedUpdate()
  {
      Accelerate();
      Yaw();
      Pitch();
      Roll();
  }
 
  void Accelerate()
  {
      //Forward and backward input
      float ForwardInput = playerInputActions.Player.Accel_Decel.ReadValue<float>();
      ForwardInput *= Time.deltaTime; //converts to per second rather than per frame
 
      //Velocity Update
      Vector3 Acceleration = ((ForwardInput * AccelSpeed) * 1) * transform.forward;
      rb.AddForce(Acceleration);
 
      //Remove X velocity
      LocalVelocity = transform.InverseTransformDirection(rb.velocity);
      LocalVelocity.x = 0;
      rb.velocity = transform.TransformDirection(LocalVelocity);
  }
 
  void Yaw()
  {
      //Yaw input
      float YawInput = playerInputActions.Player.Yaw.ReadValue<float>();
      YawInput *= Time.deltaTime; //converts to per second rather than per frame
      Vector3 Yaw = (YawInput * YawSpeed) * transform.up;
      rb.AddTorque(Yaw);
  }
 
  void Pitch()
  {
      //Pitch input
      float PitchInput = playerInputActions.Player.Pitch.ReadValue<float>();
      PitchInput *= Time.deltaTime; //converts to per second rather than per frame
      Vector3 Pitch = (PitchInput * PitchSpeed) * transform.right;
      rb.AddTorque(Pitch);
  }
 
  void Roll()
  {
      //Roll input
      float RollInput = playerInputActions.Player.Roll.ReadValue<float>();
      RollInput *= Time.deltaTime; //converts to per second rather than per frame
      Vector3 Roll = (RollInput * -RollSpeed) * transform.forward;
      rb.AddTorque(Roll);
  }

}

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by SurreyMuso · Nov 11, 2022 at 07:47 AM

Are you talking about max velocity in the Z direction or maximum of Z and Y? If both, you can use Vector3.ClampMagnitude after line 46, before you revert to world space for your rb.velocity.

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 sleepyfunnyfish · Nov 12, 2022 at 04:24 AM 0
Share

I believe I want just in the Z direction, but Y shouldn't hurt and will likely prevent potential issues. I tried this method but there was no change, below are the changes I tried. If you don't mind could you try it yourself in case im doing something wrong? Thank you.

     Vector3.ClampMagnitude(LocalVelocity, MaxSpeed);
     rb.velocity = transform.TransformDirection(LocalVelocity);

and

     rb.velocity = transform.TransformDirection(Vector3.ClampMagnitude(LocalVelocity, MaxSpeed));

as well as making a new vector variable from the ClampMagnitude copy and using that for TransformDirection

avatar image EscherK sleepyfunnyfish · Nov 12, 2022 at 06:47 AM 0
Share

In the one above you are clamping the value but not storing it

avatar image sleepyfunnyfish EscherK · Nov 12, 2022 at 06:57 PM 0
Share

I mentioned at the end of my reply that I tried storing the clamped value and using that with TransformDirection and it did not work. Ex:

      Vector3 ClampedV3 = Vector3.ClampMagnitude(LocalVelocity, MaxSpeed);
      rb.velocity = transform.TransformDirection(ClampedV3);

This had no change either.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

254 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

Related Questions

Rigidbody speed in some senes different?! 1 Answer

Add force lasting 1 frame (simple script) 1 Answer

Question regarding Ridigbody2D.velocity.magnitude 1 Answer

Making RB movement smoother/curvy. C#. 1 Answer

How to make rigidbody movement without changing or clamping the Velocity? 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