- Home /
Removing Vector3 Decimal for Position
Hey there guys, I've tried to do my research on this but had no avail, only things in relation to distance which i could not incorporate into what i wanted to achieve..
This is for a NavMesh AI that i am trying to detect weather it is moving or not, what i have so far in my Update is
if (myTransform.position != lastPosition)
anim.SetInteger("State", 1); //Walk
else
anim.SetInteger("State", 0); //Idle
lastPosition = myTransform.position;
but what happens is my AI turns side to side when it is "Idle" but the Vector position decimals are moving the slightest, so it is classing it as not in same last position.'
so how can i make the vectors drop the decimal points so it just reads whole numbers for the position?
Answer by hexagonius · Apr 22, 2017 at 02:16 PM
You can write a method that compares each component of the vectors, casting them to int first,e.g.
return (int) vector.x == (int) vector.x
&& y...
&& z...;
Answer by BloodMarked · Apr 22, 2017 at 10:07 PM
you can remove the decimal by flooring a vector:
if(Math.floor(myTransform.position) != Math.floor(lastPosition))
but this can become buggy when the character is on the edge of a full unit and switches between both values. i would reccommend using a treshold instead like p.e.
if(myTransform.position - lastPosition >= .1)
you can clean it up over time.
the best way to do this would be to track in the controller whether he is moving. going into the animation system and then read from the generated position will always be less reliable and buggy