The script, even when enabled is set to false, will still run. I want it so if inCar is false, the functions won’t run what’s inside.
Here is the script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CarGoVrum : MonoBehaviour
{
const string HORIZONTAL = "Horizontal";
const string VERTICAL = "Vertical";
float horizontalInput;
float verticalInput;
float currentSteerAngle;
float currentbreakForce;
public bool isBreaking;
public bool inCar = false;
public float motorForce;
public float breakForce;
public float maxSteerAngle;
public float trueMaxSteerAngle;
public WheelCollider frontLeftWheelCollider;
public WheelCollider frontRightWheelCollider;
public WheelCollider rearLeftWheelCollider;
public WheelCollider rearRightWheelCollider;
public Transform frontLeftWheelTransform;
public Transform frontRightWheeTransform;
public Transform rearLeftWheelTransform;
public Transform rearRightWheelTransform;
private void Update()
{
if (inCar == false)
{
frontRightWheelCollider.brakeTorque = 3000f;
frontLeftWheelCollider.brakeTorque = 3000f;
rearLeftWheelCollider.brakeTorque = 3000f;
rearRightWheelCollider.brakeTorque = 3000f;
maxSteerAngle = 0f;
}
else if (inCar == true)
{
maxSteerAngle = trueMaxSteerAngle;
}
}
private void FixedUpdate()
{
if (inCar == true)
{
GetInput();
HandleMotor();
HandleSteering();
UpdateWheels();
}
}
private void GetInput()
{
if (inCar == false)
return;
horizontalInput = Input.GetAxis(HORIZONTAL);
verticalInput = Input.GetAxis(VERTICAL);
isBreaking = Input.GetKey(KeyCode.Space);
}
private void HandleMotor()
{
if (inCar == false)
return;
frontLeftWheelCollider.motorTorque = verticalInput * motorForce;
frontRightWheelCollider.motorTorque = verticalInput * motorForce;
currentbreakForce = isBreaking ? breakForce : 0f;
ApplyBreaking();
}
private void ApplyBreaking()
{
if (inCar == false)
return;
frontRightWheelCollider.brakeTorque = currentbreakForce;
frontLeftWheelCollider.brakeTorque = currentbreakForce;
rearLeftWheelCollider.brakeTorque = currentbreakForce;
rearRightWheelCollider.brakeTorque = currentbreakForce;
}
private void HandleSteering()
{
if (inCar == false)
return;
currentSteerAngle = maxSteerAngle * horizontalInput;
frontLeftWheelCollider.steerAngle = currentSteerAngle;
frontRightWheelCollider.steerAngle = currentSteerAngle;
}
private void UpdateWheels()
{
if (inCar == false)
return;
UpdateSingleWheel(frontLeftWheelCollider, frontLeftWheelTransform);
UpdateSingleWheel(frontRightWheelCollider, frontRightWheeTransform);
UpdateSingleWheel(rearRightWheelCollider, rearRightWheelTransform);
UpdateSingleWheel(rearLeftWheelCollider, rearLeftWheelTransform);
}
private void UpdateSingleWheel(WheelCollider wheelCollider, Transform wheelTransform)
{
Vector3 pos;
Quaternion rot;
wheelCollider.GetWorldPose(out pos, out rot);
wheelTransform.rotation = rot;
wheelTransform.position = pos;
}
public void setWheelColliders(bool b)
{
frontLeftWheelCollider.enabled = b;
frontRightWheelCollider.enabled = b;
rearLeftWheelCollider.enabled = b;
rearRightWheelCollider.enabled = b;
}
}