Hello, good afternoon all.
I have a question about how should I proceed coding with the a part of my code. Well, basically the code needs to verify if all the wheels of a car is on the ground. What I’m currently doing is:
if (!carroControle.WheelFL.isGrounded()
!carroControle.WheelFR.isGrounded()
!carroControle.WheelRL.isGrounded()
!carroControle.WheelRR.isGrounded())
{
Debug.Log("Car is fliped.");
}
Well, it works fine, but I was wondering, since that part of the code is in the Update() function, would it be better to give it another approach?
I also thought about concatenate many IF’s (each one for each wheel). So the question is, in the first mode, does the IF checks for all the wheels and than it analyses it or it checks the first and if its already false it skips all the other?
I ask that because if I concatenate many IF’s than if the first one is false it would already stop the function.
This is kind theoretical about how the language behaves, but I don’t know the awnser (although I’m a programmer I didn’t do a college in this area, so these aspects I don’t know too much).
Only one quick question in this regarding. What would be the best approach for checking the previous function every 2 seconds. A InvokeRepeating() with 2 seconds of each call or a IEnumerator with a yield?
Thanks in advance,
Remedio
IF conditions will “early-out” if it evaluates a condition to be false. This is called short circuit evaluation. So if your first condition is false it won’t evaluate the others. I would probably wrap that entire evaluation into a read-only property that is part of the carroControle class just to make the Update here easier to read.
The performance of the function calls is truly negligible in this case - the performance of whatever logic you’re performing within “isGrounded()” is far more important. Unless you are doing something absolutely insane, I’d check it every tick instead of adding a delay. Seems like you’re way over-optimizing way too early.
For what it’s worth you’re probably better off putting the logic within the carroControle in this case though, something like…
// carroControle class adds this method
public bool IsFlipped(){
if (WheelFL.IsGrounded()) return false;
if (WheelFR.IsGrounded()) return false;
if (WheelRL.IsGrounded()) return false;
if (WheelRR.IsGrounded()) return false;
return true;
}
// Your current class
if (carroControle.IsFilled()){
Debug.Log("Car is flipped!");
}
This does two things:
- The behavior of deciding whether the car is flipped or not is now owned by the car, instead of an outside class.
- The performance of the method is increased. Your current method is doing a false test for all cases that need to be true, it will always run the physics check for all four wheels. By searching for the first true and immediately exiting, you’ll only ever check all four tires if the only tire grounded is the last tire - in the vast majority of situations you’ll probably just exit out of the method at the first check.
Thank you KelsoMRK and KyleStaves. I will put the method inside the carroControle indeed, it makes more sense.
shouldn’t carFlipped be something like:
CarFlipped = Vector3.Dot(transform.up, Vector3.up) < 0;
That is an interesting approach. I guess that if I change the second parameter (Vector3.up) with the relative transform.up of the road the car is in the moment I call the method it could work. I didn’t think about it. Might even be faster than checking the colliders.
I will try it!
Thanks
In checking for this, I always check to see if the vehicle is upside down and if the time being upside down is something greater than 2 seconds. This way, you can do a flip in the air and still have time to avoid a reset.
In doing AI, I specifically ask “Has the ai been in the same area for more than 3 seconds” (within 3 units, for 3 seconds == stuck)
I am sure that it is faster using the Dot product! And yes if you are driving on hill sides or something like that, then you need the Normal vector of that.
Honestly, not much will be faster than the Dot approach.
When are you driving “upside down”? Vector3.Dot(Transform.up, Vector3.up) is always positive when you are not driving “upside down”
This includes going up steep banks