- Home /
Trying to Do "X" if going up, "Y" if going down, am I using rigidbody.velocity wrong?
My gameobject flies around like a plane. I want to play a sound when it is going down, and not play that sound when it's going up. Can someone tell me what I'm doing wrong with this short C# snip? Sorry, I'm a total beginner..
if(rigidbody.velocity.y >= 0f) {
print ("going up");
}
else(rigidbody.velocity.y <= 0f) {
print ("going down");
}
Answer by GameVortex · Jan 23, 2015 at 07:48 AM
In the future please describe your problems properly. You cannot expect us to just guess what is happening on your end and what did not work. The code you have there would most certainly give you a compile error. When you get errors from code in the console, please search for around for a solution to the error first and if all else fail, post here with a description of the issue and Copy in the FULL error message (with line number).
The problem you have is that the "else" command does not work as you have written. Take a look at the documentation for if-else. To have a second condition you need to use: else if. The else command alone does not take a condition and is always the last in the if else condition tree which activates if all other conditions failed.
Also good practice to not have overlapping conditions, even with elses.
if(rigidbody.velocity.y > 0f) {
print ("going up");
}
else(rigidbody.velocity.y < 0f) {
print ("going down");
}
Would be better since you're not really "goign up" if your y velocity is zero. That's when a plane is gliding.
Your answer