- Home /
Simple Car Script - Physics makes it wierd
I am trying to create a simple car script, it works fine when driving across flat terrain but when i go over a bump of a certain height it causes the car to fly.
I originally manipulated the transform of the object to make it move, but i then tried changing the velocity of the rigidbody in a fixed update. For somereason this caused the car to firstly move in the wrong direction i.e. back instead of forwards and the car wont turn, it just rotates and flips in the continued direction.
Clearly im missing something here when it comes to manipulating rigidbody velocity but i cant figure out what it is.
Rigidbody Factors:
Mass: 1 Drag: 0.01 Angular Drag: 0.01 use gravity: true
using UnityEngine;
using System.Collections;
public class HumveeScript : MonoBehaviour {
Transform Car;
//Movement Information
public float movespeed = 0.0f;
public float reverseSpeed = 0.0f;
public float currentSpeed = 0.0f;
//Controls
public KeyCode Accelerate = KeyCode.W;
public KeyCode Decelerate = KeyCode.S;
public KeyCode SteerLeft = KeyCode.A;
public KeyCode SteerRight = KeyCode.D;
public bool hasADriver = false;
CharacterController controller;
// Use this for initialization
void Start ()
{
Car = transform;
if (rigidbody)
{
rigidbody.constraints = RigidbodyConstraints.FreezeRotationZ | RigidbodyConstraints.FreezeRotationY;
}
}
void FixedUpdate()
{
if(movespeed > 0)
{
rigidbody.velocity = Vector3.forward * movespeed;
//Car.transform.Translate(Vector3.forward * Time.deltaTime * movespeed);
}
else if(movespeed < 0)
{
rigidbody.velocity = Vector3.back * reverseSpeed;
//Car.transform.Translate(Vector3.back * Time.deltaTime * reverseSpeed);
}
}
// Update is called once per frame
void Update ()
{
if(Input.GetKey(Accelerate) && hasADriver == true)
{
if(movespeed < 0 && reverseSpeed == 0)
{
movespeed += 1;
}
else if(movespeed >= 0 && movespeed < 20)
{
movespeed += 0.5f;
}
else if(movespeed >= 20 && movespeed < 40)
{
movespeed += 1;
}
if(reverseSpeed == 0 && movespeed < 0)
{
movespeed = 0;
}
}
else
{
if(movespeed > 0 && movespeed < 50)
{
movespeed -= 1;
}
}
if(Input.GetKey(Decelerate) && hasADriver == true)
{
if(movespeed > -50)
{
movespeed -= 1;
}
if(reverseSpeed < 10 && movespeed <= 0)
{
reverseSpeed += 1;
}
}
else
{
if(reverseSpeed > 0)
{
reverseSpeed -= 1;
}
if(movespeed < 0)
{
movespeed = 0;
}
}
if (Input.GetKey(SteerLeft))
{
if (movespeed < 0)
{
Car.transform.Rotate(Vector3.up, 1.0F);
}
else if (movespeed > 0)
{
Car.transform.Rotate(Vector3.down, 1.0F);
}
}
if (Input.GetKey(SteerRight))
{
if (movespeed < 0)
{
Car.transform.Rotate(Vector3.down, 1.0F);
}
else if (movespeed > 0)
{
Car.transform.Rotate(Vector3.up, 1.0F);
}
}
}
void OnCollisionEnter ()
{
if(movespeed > 0)
{
movespeed = movespeed / 2;
}
else if(reverseSpeed > 0)
{
reverseSpeed = reverseSpeed / 2;
}
}
}
Answer by LolTroll96 · May 22, 2015 at 05:17 AM
Try changing the mass and the drag. Change the drag to about 5 or something and the mass to 2450, then see if that works. change the angular drag to about 2.5.
Your answer
Follow this Question
Related Questions
car wheels brakes 2d 1 Answer
Are y-axis wheelcolliders possible? 1 Answer
Wheel Collider - Mesh/Box Collider Problem 0 Answers
Turning Rigid body currently in contact with ground. 1 Answer
Raycast car rig going up ramps/slopes. 0 Answers