I have a garage and a car in my scene, I Want to make a script “Javascript” to check if the car is inside the Garage then do a certain function
How to make a script to check if the car is inside or not ?!
I have a garage and a car in my scene, I Want to make a script “Javascript” to check if the car is inside the Garage then do a certain function
How to make a script to check if the car is inside or not ?!
Add a trigger collider to your garage. And also there needs to be a rigidbody attached to your car. In your script define a bool variable. When the car enters the garage set the variable to true and when car leaves set it to false. Now whenever required check the value of this variable.
var isCarInside : bool = false; // We consider the car is not inside at first
function OnTriggerEnter(Collider col)
{
if(col.gameObject.name == "Car")
{
isCarInside = true;
}
}
function OnTriggerExit(Collider col)
{
if(col.gameObject.name == "Car")
{
isCarInside = false;
}
}
function Update()
{
if(isCarInside)
{
// Your car is inside and you can perform the required function here
}
}
In above script it is assumed that the name of your car game object is “Car”. You can also check it using a tag, if required.