Search Unity

Rigidbody is getting stuck on my road

Discussion in 'Physics' started by MyWifiIsLagging, May 30, 2021.

  1. MyWifiIsLagging

    MyWifiIsLagging

    Joined:
    Nov 7, 2020
    Posts:
    3
    I am building a procedurally generated road, but my cube/car is still just stopping on random occasions. The road is made out of repeating one 5/5/1 cube.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Drive : MonoBehaviour
    6. {
    7.     public float speed;
    8.     public float torque;
    9.     public float maxVelocity;
    10.    
    11.     void FixedUpdate () {
    12.         if (Time.time > 2.5) {
    13.             float moveHorizontal = Input.GetAxis ("Horizontal");
    14.             GetComponent<Rigidbody>().AddForce(transform.forward * speed * Time.deltaTime);
    15.             transform.rotation *= Quaternion.Euler(transform.up * moveHorizontal * torque * Time.deltaTime);
    16.         }
    17.  
    18.         if (GetComponent<Rigidbody>().velocity.z > maxVelocity) {
    19.             GetComponent<Rigidbody>().velocity -= new Vector3(0, 0, GetComponent<Rigidbody>().velocity.z-maxVelocity);
    20.         }
    21.     }
    22. }
    23.  
     

    Attached Files: