Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Limit speed on local axis

Discussion in 'Physics' started by Bricksmashr10, Oct 14, 2023.

  1. Bricksmashr10

    Bricksmashr10

    Joined:
    Oct 5, 2023
    Posts:
    24
    Hello everyone, I've been programming a 3d car racing game in N64 style (hopefully I can end up with it on a N64) but I've run into a problem, I want to limit the speed on the local Z. Help would be much appreciated. thanks in advance.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class carDrive : MonoBehaviour
    6. {
    7.     public float speed;
    8.     public float agility;
    9.     public Rigidbody rigidbody;
    10.     //public float maxspeed;
    11.     // Start is called before the first frame update
    12.     void Start()
    13.     {
    14.         rigidbody = GetComponent<Rigidbody>();
    15.     }
    16.  
    17.     // Update is called once per frame
    18.     void Update()
    19.     {
    20.         float h = Input.GetAxisRaw("Horizontal") * agility;
    21.        
    22.         rigidbody.AddRelativeForce(Vector3.forward * speed);
    23.  
    24.         /*if (rigidbody.velocity.x > maxspeed)
    25.         {
    26.             rigidbody.velocity = new Vector3(maxspeed, rigidbody.velocity.y, rigidbody.velocity.z);
    27.         }
    28.        
    29.         if (rigidbody.velocity.y > maxspeed)
    30.         {
    31.             rigidbody.velocity = new Vector3(rigidbody.velocity.x, maxspeed, rigidbody.velocity.z);
    32.         }
    33.  
    34.         if (rigidbody.velocity.z > maxspeed)
    35.         {
    36.             rigidbody.velocity = new Vector3(rigidbody.velocity.x, rigidbody.velocity.y, maxspeed);
    37.         }*/
    38.  
    39.         transform.RotateAround(transform.position, transform.up, Time.deltaTime * h);
    40.     }
    41. }
    42.  
     
  2. Deleted User

    Deleted User

    Guest

    Last edited by a moderator: Oct 14, 2023
  3. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    756
    You can increase the drag setting.
     
  4. Deleted User

    Deleted User

    Guest

    I experimented for myself with the answer given on Discussions and found a way. The script below works both ways, if the speed is below the maximum speed or if it is above that speed.
    The commented code at the bottom is a way to achieve speed limit that Unity Technologies doesn't, apparently, recommend.

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class RigidbodyVelocityLimitTest : MonoBehaviour
    4. {
    5.     [SerializeField] private float speed, maxSpeed;
    6.  
    7.     private Rigidbody rigidBody;
    8.     private float velocityX, velocityY, brakeForce;
    9.  
    10.     private void Start()
    11.     {
    12.         // Accessing the rigidbody
    13.         rigidBody = GetComponent<Rigidbody>();
    14.         // Storing velocities
    15.         velocityX = rigidBody.velocity.x;
    16.         velocityY = rigidBody.velocity.y;
    17.     }
    18.  
    19.     private void FixedUpdate()
    20.     {
    21.         // Moving the vehicle
    22.         VehicleMovement();
    23.     }
    24.  
    25.     private void VehicleMovement()
    26.     {
    27.         if (Input.GetKey(KeyCode.UpArrow))
    28.         {
    29.             rigidBody.velocity = new Vector3(velocityX, velocityY, speed);
    30.  
    31.             // Making sure the vehicle speed is limited
    32.             // My way
    33.             // Getting the rigidbody velocity in unit per second
    34.             float magnitude = rigidBody.velocity.magnitude;
    35.             if (magnitude != maxSpeed)
    36.             {
    37.                 rigidBody.velocity = new Vector3(velocityX, velocityY, brakeForce);
    38.                 float diffSpeed = magnitude - maxSpeed;
    39.                 switch (diffSpeed)
    40.                 {
    41.                     case > 0:
    42.                         brakeForce = magnitude - diffSpeed;
    43.                         break;
    44.                     case < 0:
    45.                         brakeForce = maxSpeed;
    46.                         break;
    47.                     default:
    48.                         break;
    49.                 }
    50.             }
    51.         }
    52.     }
    53.  
    54.     // Apparently non-recommended way of achieving speed limit
    55.     /*private void SpeedLimit()
    56.     {
    57.         if (rigidBody.velocity.magnitude > maxSpeed)
    58.         {
    59.             rigidBody.velocity = rigidBody.velocity.normalized * maxSpeed;
    60.         }
    61.     }*/
    62. }
    But, if you want, you can get to the wanted speed directly:

    Code (CSharp):
    1.     private void VehicleMovement()
    2.     {
    3.         if (Input.GetKey(KeyCode.UpArrow))
    4.         {
    5.             rigidBody.velocity = new Vector3(velocityX, velocityY, maxSpeed);
    6.         }
    7.     }
     
    Last edited by a moderator: Oct 15, 2023
  5. Bricksmashr10

    Bricksmashr10

    Joined:
    Oct 5, 2023
    Posts:
    24
    I tried that but found it hard to get the right acceleration - speed ratio. doesn't give the feel I want.
     
  6. Bricksmashr10

    Bricksmashr10

    Joined:
    Oct 5, 2023
    Posts:
    24
    thanks for the help. the it's working great now.