D
D
Dmitry Gavrilenko2015-06-25 10:47:48
C++ / C#
Dmitry Gavrilenko, 2015-06-25 10:47:48

Unity3D 5 - Speed, direction, physics?

The character has 2d physics. The character has a script component. Everything works, except for one section.

void FixedUpdate()
    {
        if (velocity.y > 0.1)
        {
            Debug.Log("Up Jump");
            this.GetComponent<SpriteRenderer>().sprite = this.upJump;
        }
        else if (velocity.y < 0.1)
        {
            Debug.Log("Down Jump");
            this.GetComponent<SpriteRenderer>().sprite = this.downJump;
        }
        else if (velocity.y == 0.0)
        {
            Debug.Log("NOT Jump");
        }
    }

That is, the character jumps, the sprites change, but when he stands still and in the velocity.y = 0 inspector, the message "NOT Jump" is not displayed on the console. Why?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Lastochkin, 2015-06-25
@Maddox

Rule: never compare real numbers for equality (e.g. representation errors, rounding errors, etc.)

...
else if ( Math.Abs(velocity.y) <= 0.1 )
{
       Debug.Log("NOT Jump");
}

Also: it is better to take out the constant

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question