Question by
ars_longa · May 04, 2020 at 08:04 PM ·
if-statementsif statementupdate function
Using "if" statements to designate movement limits
Hello peeps!
I'm trying to make an object wobble - that is, continuously move up and down inside a given range. I could achieve that with a loop, but I wanted to see if I could use "if" and the fact that "update" is effectively a loop already. Here's what I did:
void Update()
{
wobble();
}
void wobble()
{
if (transform.position.y == wobbleHighLim)
{
wobbleSpeed = wobbleSpeed * -1;
print (wobbleSpeed);
}
if (transform.position.y == wobbleLowLim)
{
wobbleSpeed = wobbleSpeed * -1;
print (wobbleSpeed);
}
transform.Translate(Vector3.up * wobbleSpeed * Time.deltaTime, Space.World);
}
I positioned the object inside the range and hit play. It started going up and never stopped, no message in the console either. However, when I changed the conditions from "if (transform.position.y == wobbleHighLim)" to "if (transform.position.y >= wobbleHighLim)" (and the same for wobbleLowLim), everything worked. Could someone please explain why that happened and what's the difference?
Comment
Your answer
