- Home /
Question by
stevinh · Sep 10, 2020 at 02:15 AM ·
if-statementscondition
Display message when max height is reached
I am trying to display a message when the vector reaches the max height (maxVertical) but it's not displaying the message. Any ideas what I'm doing wrong?
position[1] += yPosition;
this.transform.position = position;
if (position[1] == maxVertical)
{
Debug.Log("Max Height");
}
where yPosition = 0.1f, position = (0,0,0), maxVertical = 50f
Comment
Answer by Namey5 · Sep 10, 2020 at 10:04 AM
With floating point accuracy and accounting for time, it is incredibly unlikely that this condition will ever be reached. Instead, it's probably better to check within a region, i.e.
if (position[1] < maxVertical)
{
position[1] += yPosition;
if (position[1] >= maxVertical)
{
position[1] = maxVertical;
Debug.Log("Max Height");
}
this.transform.position = position;
}