Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Car control script strange movement

Discussion in 'Scripting' started by Epic-Username, Jun 18, 2015.

  1. Epic-Username

    Epic-Username

    Joined:
    May 24, 2015
    Posts:
    339
    I have made this script to control a car in the game, I'm using rb.AddForce to make it move which i think is not a good movement method for a car in the game and in the game the acceleration is slow then accelerates to about 200 in 1 sec all of a sudden which is unrealistic, So my questions is which is the best line of code for car movement. i have also tried the vector3.forward but then it flies around in the air like crazy and it has unrealistic physics then.


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class car : MonoBehaviour {
    6.  
    7.     public float thrust;
    8.     public Rigidbody rb;
    9.  
    10.     public Text speedText;
    11.  
    12.     private float speed;
    13.    
    14.     public float drag;
    15.     public float acceleration;
    16.     public float breakpower;
    17.  
    18.     void Start () {
    19.         rb = GetComponent<Rigidbody>();
    20.     }
    21.  
    22.     void Update () {
    23.  
    24.         speedText.text = Mathf.Round(rb.velocity.magnitude * 3) + " kph";
    25.         speed -= (drag * (speed / (drag * 75)));
    26.  
    27.         rb.AddForce(-transform.forward * speed  * Time.deltaTime);
    28.         if(Input.GetKey ("up")) {
    29.             speed += acceleration;
    30.         }
    31.  
    32.         if(Input.GetKey ("down")) {
    33.             speed-= breakpower;
    34.         }
    35.         if(Input.GetKey ("right")) {
    36.             transform.Rotate(new Vector3(0.0f,1.0f,0.0f), (3));
    37.         }
    38.         if(Input.GetKey ("left")) {
    39.             transform.Rotate(new Vector3(0.0f,-1.0f,0.0f), (3));
    40.         }
    41.     }
    42. }
    43.  
     
  2. magnetix

    magnetix

    Joined:
    Apr 4, 2010
    Posts:
    102
    If you want you car to respond realistically under force, you need to use Newton's Second Law: F=ma

    For this you should set your game world up to the correct scale with realistic values. So the car must have a mass (m) and you decide how fast you want your car to accelerate (a). If your car accelerates from 0 m/s to 10 m/s over a time of 5 seconds, then your acceleration is 2 m/s/s (it is 2 m/s faster each second). So, set the mass on your rigidbody, then the force you apply to this is that mass multiplied by 2 multiplied by the time step.

    Make sure you use the correct ForceMode in the AddForce method (see scripting ref). Honestly, I've been caught out with this before and can't immediately remember which is the correct mode for this situation.

    The alternative to all of this is to do your car position updates yourself using the kinematic mode on the rigidbody (or even remove the RB altogether). Using your knowledge of physics, you should probably be able to work out how to do this.