- Home /
 
Unity relative velocity limiting
Ive been working on a 3d first person and need to have velocity limiting. The player is rotated with the mouse, this caused issues as I need the velocity to be slowed based of the player object's x and z axis not the world x and z axis, however there is unfortunately no function for this
here is my code: using System.Collections; using System.Collections.Generic; using UnityEngine;
 public class Player : MonoBehaviour {
     //player speed
     public float speed;
     public float jumpPower;
     public float velLimit;
 
     //velocity limiting;
     private Vector3 relativeVel = new Vector3(0, 0, 0);
 
     // looking
     private float rotY = 0;
     public float mouseSensitivity;
 
     //body
     public Rigidbody rBody;
 
     //keys
     public KeyCode left;
     public KeyCode right;
     public KeyCode forward;
     public KeyCode backward;
 
     public KeyCode jump;
 
     void Update () {
         //velocity limiting
         relativeVel = transform.TransformDirection (rBody.velocity);
 
         if(relativeVel.x > velLimit){
             Vector3 vel = new Vector3 (velLimit, rBody.velocity.y, rBody.velocity.z);
             rBody.velocity = vel;
         }
             if(relativeVel.z > velLimit){
             Vector3 vel = new Vector3 (rBody.velocity.z, rBody.velocity.y, velLimit);
             rBody.velocity = vel;
         }
         //negative
             if(relativeVel.x < -velLimit){
             Vector3 vel = new Vector3 (-velLimit, rBody.velocity.y, rBody.velocity.z);
             rBody.velocity = vel;
         }
             if(relativeVel.z < -velLimit){
             Vector3 vel = new Vector3 (rBody.velocity.z, rBody.velocity.y, -velLimit);
             rBody.velocity = vel;
         }
 
         //movement
         if(Input.GetKey(left)){
             Vector3 leftMove = new Vector3 (-1 * speed, 0, 0);
             rBody.AddRelativeForce (leftMove);
         }
 
         if(Input.GetKey(right)){
             Vector3 rightMove = new Vector3(1 * speed, 0, 0);
             rBody.AddRelativeForce (rightMove);
         }
 
         if(Input.GetKey(forward)){
             Vector3 forwardMove = new Vector3(0, 0, 1 * speed);
             rBody.AddRelativeForce (forwardMove);
         }
 
         if(Input.GetKey(backward)){
             Vector3 backwardMove = new Vector3(0, 0, -1 * speed);
             rBody.AddRelativeForce (backwardMove);
         }
 
         if(Input.GetKeyDown(jump)){
         Vector3 jumpMove = new Vector3(0,jumpPower,0);
             rBody.AddRelativeForce (jumpMove);
         }
 
         //looking
         rotY += Input.GetAxis ("Mouse X") * mouseSensitivity;
         Vector3 rot = new Vector3 (0, rotY, 0);
         transform.eulerAngles = rot;
     }
 }
 
 
              
               Comment
              
 
               
              Your answer
 
             Follow this Question
Related Questions
Rigidbody character controller can't walk on stairs 0 Answers
Moving a player with Rigidbody 2 Answers
Problem Trying to Apply Non-Kinematic Velocity to Rigidbody 0 Answers
GameObject disappears when velocity is set to (near) zero. 1 Answer
Simulating Fighter Jet Physics (with addForce () ) 2 Answers