This site uses strictly necessary cookies. More Information

X- Home /

# Mathf.Abs and How to play animation only if x,z transform has changed by 0.2 units ?

Hello all,

I'm using HTC VIVE for a multi user game, and there's an avatar that is following the cameraEye component, acting like a third controller person.

I want the walking animation to play if cameraEye is moving and Idle animation to play if cameraEye is Idle. The thing is, cameraEye is never really idle, and its transorm is alway changing a little bit.

I want to say that **only** if position has changing **x+-0.2, z +-0.2**, then play animation, else keep idle.

EDIT: I'm trying this code but it's not really working:

```
private void Update()
{
if ((Mathf.Abs(currentPosition.x) - Mathf.Abs( lastPosition.x) >=0.2f) || (Mathf.Abs(currentPosition.x) - Mathf.Abs(lastPosition.x) <= 0.2f) && (Mathf.Abs(currentPosition.z)- Mathf.Abs(lastPosition.z) >= 0.2f) || (Mathf.Abs(currentPosition.z) - Mathf.Abs(lastPosition.z) <= 0.2f))
{
Vector3 currentPosition = transform.position;
if (currentPosition != lastPosition)
{
playerAnimator.SetBool("Walk", true);
lastPosition = currentPosition;
}
else
{
playerAnimator.SetBool("Walk", false);
}
}
```

say I have a transform.x

transform.x = idle

but

transform.x+0.2f || tranform.x-0.2f = walk

**Answer** by Krish-Vikram
·
Aug 01, 2017 at 10:37 AM

I Think the condition should be as follows..

```
if ( Mathf.Abs(currentPosition.x - lastPosition.x) ==0.2f &&
Mathf.Abs(currentPosition.z - lastPosition.z) == 0.2f )
```

P.S: 1. Use && or || according your expected logic. 2. Rounding off the currentPosition, lastPosition might be better if you need exactly 0.2f difference.

**Answer** by Eco-Editor
·
Aug 01, 2017 at 10:56 AM

What I need is if the player moves 0.2 or higher on the x and/or z axis than this indicates he's moving. Else, if the delta is smaller, this means the player didn't really moved, maybe leaned forward and so play idle animation.

Also I do need this entire raw, like that:` if ((Mathf.Abs(currentPosition.x) - Mathf.Abs( lastPosition.x) >=0.2f) || (Mathf.Abs(currentPosition.x) - Mathf.Abs(lastPosition.x) <= 0.2f) && (Mathf.Abs(currentPosition.z)- Mathf.Abs(lastPosition.z) >= 0.2f) || (Mathf.Abs(currentPosition.z) - Mathf.Abs(lastPosition.z) <= 0.2f)) `

because I want the current and the last positions to be absolute and not just the delta. Otherwise I might get negative number that will sum up to be a very high positive number. @Krish-Vikram Perhaps I'll change the && to be ||.

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.