- Home /
Set a boolean to true once the wheel collides with two planes?
Hello, I have set a wheel collider to a car, and i have placed two planes along the ground which are tagged with "tag1" and "tag2". What i want to do is, I want to set a boolean variable to true, once the wheel passes through or collides with both the planes. Here's the code I've written, but it doesn't seem to work, donno why:
var isFinished : boolean = false;
var myWC : WheelCollider;
function Update () {
var hit : WheelHit;
if(myWC.GetGroundHit(hit)) {
if(hit.collider.gameObject.tag == "tag1"){
if(hit.collider.gameObject.tag == "tag2") {
isFinished = true;
}
}
}
}
What are these? waypoints on a course? a parking spot? Is there a separate collider on each wheel?
Looks to me like these can never both be true in the same frame unless the two planes are overlapping.
well, these are planes as i mentioned above. What I want to achieve is, I'm making a racing game; I have made two planes as the finish line, the car should move over these two planes in sequential order i.e. first it should move over tag1 plane and then tag2 plane, then i want to set the isFinished (which indicates that the race is finished) to true. And yeah these are not overlapping.
Answer by Robble · Apr 19, 2013 at 09:16 PM
You are checking if the wheel has hit tag1 and tag2 at the same time as it will only hit one at a time the function will never return true.
I've tweaked your code so it should work but i would point out this code is very ugly and inefficient but its the best i can do without knowing more about how your project works. hopefully it will set you moveing in the right direction
var isFinished : boolean = false;
var myWC : WheelCollider;
private var tag1Hit:boolean = false;
private var tag2Hit:boolean = false;
function Update () {
var hit : WheelHit;
if(myWC.GetGroundHit(hit)) {
if(hit.collider.gameObject.tag == "tag1"){
tag1Hit =true;
}
if(hit.collider.gameObject.tag == "tag2") {
tag2Hit =true;
}
}
isFinished = CaculateIsFinished();
}
function CaculateIsFinished():boolean{
if(tag1Hit && tag2Hit){
return true;
}else{
return false;
}
}
What I want to achieve is, I'm making a racing game; I have made two planes as the finish line, the car should move over these two planes in sequential order i.e. first it should move over tag1 plane and then tag2 plane, then i want to set the isFinished (which indicates that the race is finished) to true. And yeah these are not overlapping.