- Home /
How can I get my car to stop moving?
Hello! I have a car script and I need to diable it (its a bus and once one 'ride' is over, it needs to stop at the starting point). Here is my code (comment where I tried to make it stop):
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class CarEngine : MonoBehaviour
{
public Transform path;
public float maxSteerAngle = 45f;
public float turnSpeed = 5f;
public WheelCollider wheelFL;
public WheelCollider wheelFR;
public WheelCollider wheelRL;
public WheelCollider wheelRR;
public float maxMotorTorque = 80f;
public float maxBrakeTorque = 150f;
public static float currentSpeed;
public float maxSpeed = 100f;
public Vector3 centerOfMass;
public bool isBraking = false;
public Texture2D textureNormal;
public Texture2D textureBraking;
public Renderer carRenderer;
public CarEngine thisScript;
private List<Transform> nodes;
private int currectNode = 0;
private float targetSteerAngle = 0;
private void Start()
{
GetComponent<Rigidbody>().centerOfMass = centerOfMass;
thisScript = GetComponent<CarEngine>();
Transform[] pathTransforms = path.GetComponentsInChildren<Transform>();
nodes = new List<Transform>();
for (int i = 0; i < pathTransforms.Length; i++)
{
if (pathTransforms[i] != path.transform)
{
nodes.Add(pathTransforms[i]);
}
}
}
private void FixedUpdate()
{
ApplySteer();
Drive();
CheckWaypointDistance();
Braking();
LerpToSteerAngle();
}
private void ApplySteer()
{
Vector3 relativeVector = transform.InverseTransformPoint(nodes[currectNode].position);
float newSteer = (relativeVector.x / relativeVector.magnitude) * maxSteerAngle;
targetSteerAngle = newSteer;
}
private void Drive()
{
currentSpeed = 2 * Mathf.PI * wheelFL.radius * wheelFL.rpm * 60 / 1000;
if (currentSpeed < maxSpeed && !isBraking)
{
wheelFL.motorTorque = maxMotorTorque;
wheelFR.motorTorque = maxMotorTorque;
}
else
{
wheelFL.motorTorque = 0;
wheelFR.motorTorque = 0;
}
}
private void CheckWaypointDistance()
{
int count = 0;
if (Vector3.Distance(transform.position, nodes[currectNode].position) < 6f)
{
if (currectNode == nodes.Count - 1)
{
currectNode = 0;
count++;
}
else
{
currectNode++;
count++;
}
}
if (count > 2 && currectNode ==1) //SHOULD BE STOPPING IT????????
{
currectNode = 0;
isBraking = true;
currentSpeed = 0;
thisScript.enabled = false;
}
}
private void Braking()
{
if (isBraking)
{
carRenderer.material.mainTexture = textureBraking;
wheelRL.brakeTorque = maxBrakeTorque;
wheelRR.brakeTorque = maxBrakeTorque;
}
else
{
carRenderer.material.mainTexture = textureNormal;
wheelRL.brakeTorque = 0;
wheelRR.brakeTorque = 0;
}
}
private void LerpToSteerAngle()
{
wheelFL.steerAngle = Mathf.Lerp(wheelFL.steerAngle, targetSteerAngle, Time.deltaTime * turnSpeed);
wheelFR.steerAngle = Mathf.Lerp(wheelFR.steerAngle, targetSteerAngle, Time.deltaTime * turnSpeed);
}
}
Comment
Answer by Vollmondum · Mar 21, 2019 at 07:51 AM
You need to reset your wheelFL.rpm to 0, not currentSpeed. Current speed depends on wheel RPMs
Your answer
Follow this Question
Related Questions
Make a Lapping System for a Car Game 1 Answer
player to kill ai 0 Answers
is there a good ai car tutorial about 2 Answers
Creating enemy animation in unity 3d 0 Answers
make ai variant of car script 0 Answers