- Home /
The question is answered, right answer was accepted
vehicle not accelerating after braking
hi, am learning the movement of vehicles using wheel colliders. i wrote a small script to move a car and when i apply brake to it the car stops and doesn't accelerate anymore. need help on how to once again accelerate.
using UnityEngine;
using System.Collections;
public class new_car : MonoBehaviour {
public float maxTorque = 50.0f;
public float minTorque = 50.0f;
//public float maxSteer;
public WheelCollider[] wheelColliders = new WheelCollider[4];
public Transform[] wheels = new Transform[4];
public Transform centerOfMass;
private Rigidbody n_rigidBody;
//bool brakeApplied = false;
// Use this for initialization
void Start ()
{
n_rigidBody = GetComponent<Rigidbody>();
n_rigidBody.centerOfMass = centerOfMass.localPosition;
}
// Update is called once per frame
void Update ()
{
UpdateMeshes ();
}
void FixedUpdate()
{
float steer = Input.GetAxis ("Horizontal");
float accelerate = Input.GetAxis ("Vertical");
float maxSteer = steer * 30.0f;
wheelColliders [0].steerAngle = maxSteer;
wheelColliders [1].steerAngle = maxSteer;
if (Input.GetKeyDown (KeyCode.UpArrow) || Input.GetKeyDown (KeyCode.DownArrow))
{
for(int i = 0; i < 4; i++)
{
wheelColliders[i].motorTorque = accelerate * maxTorque;
}
}
if(Input.GetKeyDown(KeyCode.Space))
{
for(int i = 0; i < 4; i++)
{
wheelColliders[i].brakeTorque = minTorque;
}
}
}
void UpdateMeshes()
{
for(int i = 0; i < 4; i++)
{
Quaternion quat;
Vector3 pos;
wheelColliders[i].GetWorldPose(out pos,out quat);
wheels[i].position = pos;
wheels[i].rotation = quat;
}
}
}
I have no experience in car scripting but from what i can see you set the brake torque however when you are done braking the brakiøe torque is still set to that value. Try setting the torque back when you are done braking. Same with acceleration
Answer by galaboy · Aug 10, 2015 at 12:14 PM
thanks for the guys for trying to help me. i figured out the answer. below is the answer i was expecting. once again thanks for the unity community.
using UnityEngine;
using System.Collections;
public class Car : MonoBehaviour
{
public float maxTorque = 50f;
public float brakeTorque = 50f;
public Transform centerOfMass;
public WheelCollider[] wheelColliders = new WheelCollider[4];
public Transform[] tireMeshes = new Transform[4];
private Rigidbody m_rigidBody;
void Start()
{
m_rigidBody = GetComponent<Rigidbody>();
m_rigidBody.centerOfMass = centerOfMass.localPosition;
}
void Update()
{
UpdateMeshesPositions();
}
void FixedUpdate()
{
float steer = Input.GetAxis("Horizontal");
float accelerate = Input.GetAxis("Vertical");
float finalAngle = steer * 3.0f;
wheelColliders[0].steerAngle = finalAngle;
wheelColliders[1].steerAngle = finalAngle;
for (int i = 1; i < 4; i++)
{
wheelColliders[i].motorTorque = accelerate * maxTorque;
//wheelColliders[3].motorTorque = accelerate * maxTorque;
}
if (Input.GetKeyDown (KeyCode.Space))
{
for(int i = 0; i < 4; i++)
{
wheelColliders[i].brakeTorque = brakeTorque;
}
}
if (Input.GetKeyUp (KeyCode.Space))
{
for(int i = 0; i < 4; i++)
{
wheelColliders[i].brakeTorque = 0.0f;
}
}
}
void UpdateMeshesPositions()
{
for(int i = 0; i < 4; i++)
{
Quaternion quat;
Vector3 pos;
wheelColliders[i].GetWorldPose(out pos, out quat);
tireMeshes[i].position = pos;
tireMeshes[i].rotation = quat;
}
}
}
Thanks for solution! :) I found this problem to and you helped me a lot.....
Answer by imilanspinka · Aug 06, 2015 at 03:31 PM
I don't know, but
if (Input.GetKeyDown (KeyCode.Space)) {
for (int i = 0; i < 4; i++)
{
wheelColliders [i].brakeTorque = maxTorque - 40.0f;
}
else
for(int i = 0; i < 4; i++)
{
wheelColliders [i].brakeTorque = 0;
wheelColliders [i].motorTorque = accelerate * maxTorque;
}
}
?