I have a cartoon car and it has a skid sound with the car skids. But when the car flips over the skid sound plays endlessly and its really annoying. I have a basic script that is meant to detect when the car is upright and only play the skid sound with the car is upright. Here is the code:
This script is on the car itself:
public bool IsUpright () {
Vector3 rotationInEuler = transform.rotation.eulerAngles;
float tiltX = Mathf.Abs(rotationInEuler.x);
float tiltZ = Mathf.Abs(rotationInEuler.z);
if(tiltX < uprightThreshold && tiltZ < uprightThreshold){
return true;
}else{
return false;
}
}
This script is on a skid sound prefab:
void Update () {
if(motor.IsUpright()){
if(motor.IsMoving()){
transform.GetComponent<WheelCollider>().GetGroundHit(out tireHit);
currentFrictionValue = Mathf.Abs(tireHit.sidewaysSlip);
if(skidAt <= currentFrictionValue && skidWait <= 0){
Instantiate(skidSound, tireHit.point, Quaternion.identity);
skidWait = 1;
}
skidWait -= Time.deltaTime * skidEmission;
}
}
}
When the IsUpright bool (first script) is printed to the console it keeps flickering between true and false and it isn't detecting when the car is upright. By the way the uprightThreshold from the first script has a value of 40. I have also check in the inspector and the rotation of the car on both the x and z axis are not constant at 0, it is flickering up and down between about 1 and -1, but unfortunately unity doesn't support having negative rotation values, so rather it goes to 359, which is mucking up the system. Could someone show me how to fix this? Thanks in advance.