Outside of OverlapSphere

Hi,

The Problem is still Buggy: When the player is outside of the radius, when the Player stops, turns around (or move backwards), and then proceeds toward the radius, my varaible still decrements! Bad. If I am outside of the radius, and proceed closer to the radius, I need to be incrementing...

I set up the below Vector3.Distance with if else blocks because I think it works more to my expectations than overlap sphere!

`Vector3 lastPosition, oldPosition;
Vector3 vecToTarget = GameObject.Find(“Object1”).transform.position - lastPosition;

if (Vector3.Dot(lastPosition.normalized, vecToTarget.normalized) > 0)
{
    if (! pause_variable && variable <= 1)
    {
        //incrememnt varaible
    }
}
else
{
    if (! pause_variable && variable > 0)
    {
        //increment variable
    }

    if (Vector3.Distance(oldPosition, transform.position) < 0.90f)
    {
        pause_variable = true;
    }

    if (Vector3.Distance(oldPosition, transform.position) > 0.90f)
    {
        pause_variable = false;
    }

}

oldPosition = transform.position;

lastPosition = transform.position;

`

What I mean by buggy, is that when the player moves away from the object, rotates 180 degrees, then moves back toward the object, the varaible decrements.....when it should increment. I have tested this thoroughly and what I am telling is what unfortunately happens.

It would also be nice, if I could somehow use the radius like in OverlapSphere, that way I don't have to get incredibly close to the object for my varaible to increment.

The problem with your logic and your desired results doesn't seem to be the overlap sphere bit, but you need the logic for "am I moving towards the sphere".

For that there's an easy solution.

Assuming you have the forward vector of the player's movement, you can do something like this.

Vector3 vecToTarget = GameObject.Find("Object_1").transform.position - player.transform.position;

if( Vector3.Dot( player.movement.forward.normalized, vecToTarget.normalized ) > 0 )
{
    // player is moving towards the target
}
else
{
    // player is moving away from the target
}

Although not Unity documentation, a good resource on the dot product can be found here: http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.vector3.dot.aspx