- Home /
Question by
nazreee · Apr 17, 2017 at 07:25 PM ·
collideraudioontriggerenterontriggerexitontriggerstay
Audio not playing after OnTriggerStay is applied to the scripts (Driving Simulator Project)
Hello. I am making a driving simulator for my project.
The code below is for Speed Limit scene.
When drivers drive within a certain speed, they will be given warnings to reduce their speed if they break the speed limit.
Here I am using colliders. Everything is working fine, however I found out that the Audio is not playing.
This happen after I added OnTriggerStay onto the codes, can anyone check my codings if anything is wrong? Thank you. The deadline for this project is next week.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class SpeedWarning : MonoBehaviour {
float carSpeed;
private GameObject car;
private RCCCarControllerV2 carController;
private Rigidbody rigid;
public Text slowDown;
public Text good;
public AudioSource SpeedWarningSound;
//public float goodTime = 2;
void Start(){
carController = GetComponent<RCCCarControllerV2> ();
SpeedWarningSound = GetComponent<AudioSource> ();
rigid = GetComponent<Rigidbody>();
}
void OnTriggerEnter (Collider car) {
if (car.gameObject.name == ("40kmh_Collider")) {
if (carController.speed > 40) {
SpeedWarningSound.Play ();
slowDown.enabled = true;
good.enabled = false;
Debug.Log ("Laju");
} else if (carController.speed < 40) {
SpeedWarningSound.Stop ();
slowDown.enabled = false;
good.enabled = true;
//Destroy (good, goodTime);
Debug.Log ("masuk ikut speed limit ni");
//good
}
}
}
void OnTriggerStay (Collider car) {
if (car.gameObject.name == ("40kmh_Collider")) {
if (carController.speed > 40) {
//StartCoroutine (PlayWarn ());
SpeedWarningSound.Play ();
slowDown.enabled = true;
good.enabled = false;
Debug.Log ("Increase ya belaju");
} else if (carController.speed < 40) {
//StartCoroutine (StopWarn ());
SpeedWarningSound.Stop ();
slowDown.enabled = false;
good.enabled = true;
//Destroy (good, goodTime);
Debug.Log ("ubahnya pelahan");
//good
}
}
}
void OnTriggerExit (Collider car) {
if (car.gameObject.name == ("40kmh_Collider")) {
SpeedWarningSound.Stop ();
slowDown.enabled = false;
good.enabled = false;
Debug.Log ("yes");
} else {
}
}
}
Comment