Hi,

I am trying to use the logic for, "is the player moving toward an object". When the player gets close to the object, increment a varaible, else decrement. Seems simple enough.

I've tried using Vector3.Distance with my Players Position and the Object's position, it seems that, when I am outside of the distance, and rotate 180 degrees, than proceed toward the object the varaible decrements when it should increment.

I need to know when the player is moving toward the object or away from the object (facing in any direction). Below is a snippet of code to better illustrate what I am trying to acheive.

```
`void Move_Player()
{
MoveForward = Input.GetAxis(“Vertical”)  * MoveSpeed * Time.deltaTime;
MoveRotate = Input.GetAxis(“Horizontal”) * RotateSpeed * Time.deltaTime;
```// Move the player
transform.Translate(Vector3.forward * MoveForward);
transform.Rotate(Vector3.up * MoveRotate);
```
}
void foo()
{
if (rigidbody.velocity.sqrMagnitude < 0.001f)
{
Debug.Log(“Not Moving”);
}
else if (rigidbody.velocity.sqrMagnitude > 0.001f)
{
Debug.Log(“Moving”);  // doesn’t output.
}
}
````

Get rid of the distance checks, get rid of the pause variable, and simply debug.log inside the first if statement "moving towards" and in the second one "moving away". Play the game and see if the output is as expected.

If it is, you have some other logic error.

Also, yes, if it's attached to the player you can just use transform.position or transform.forward without having to GameObject.Find itself.

Edit: Also, transform.forward only takes into account the direction the object is facing, not the direction the object is moving. It's more correct to use something like characterController.velocity.normalized or rigidbody.velocity.normalized instead of transform.forward.

You'll have to handle the edge case where velocity.sqrMagnitude ~= 0.

Edit2: Do this, and store off a variable to "Object_1" since Find() is slow to do every frame

Edit3: change with how you're moving the object

``````Vector3 forward;
if( Mathf.Abs( MoveFoward ) < .00001f )  // close to zero
forward = Vector3.zero
else if( MoveForward < 0.0f )
forward = -transform.forward;
else
forward = transform.forward;

if( forward.sqrMagnitude < .001f )
{
Debug.Log( "Not moving" );
}
else
{
Vector3 vecToTarget = object_1.transform.position - transform.position;
if (Vector3.Dot(forward, vecToTarget.normalized) > 0)
{
Debug.Log("Moving Toward");
}
else
{
Debug.Log("Moving Away");
}
}

``````