- Home /
Change Variable on Trigger Enter/Exit
I want to change the currentSpeed variable in my AICarScript.If the player enters the trigger i want to change the currentspeed to 0 and if there exit them they will change back to 40,but how could i do that here is my script... got anyone an idea? THX
function OnTriggerEnter (other: Collider) {
if (!other.CompareTag("Player")) return;
other.GetComponent(AICarScript).currentSpeed -= 40;{
}
}
function OnTriggerExit (other: Collider) {
if (!other.CompareTag("Player")) return;
other.GetComponent(AICarScript).currentSpeed = 40;{
}
}
...and the trigger is a part of the AI Car Gameobject.
Supposing this script is attached to AI Car Object on which AICarScript is also attached. And you want its ( AI Car Object) speed to be zero when player enter its collider and back to 40 when player exits its collider.
function OnTriggerEnter (other: Collider)
{
if (other.CompareTag("Player"))
{
GetComponent(AICarScript).currentSpeed = 0;
}
}
function OnTriggerExit (other: Collider)
{
if (other.CompareTag("Player"))
{
GetComponent(AICarScript).currentSpeed = 40;
}
}
priyanshu Is Right . check that trigger component have "istrigger" check box , checked !
Your answer
Follow this Question
Related Questions
Change static variable in JS through Csharp 1 Answer
Static Variables between Scenes 1 Answer
Getting a Trigger's parent 1 Answer
Using variables from colliders across scripts 1 Answer
How to change Target of camera from Standard Assets 1 Answer