Proximity checker

Hi everyone,

I’m doing the Challenge: Moving sound and in the example game, there’s an object that starts moving when the player gets close to it.

So my question is: How do I check when the player is close enough to trigger the action that I want?

public class RiseAndRun : MonoBehaviour  
{  
    public Vector3 positionChange;  
    public Vector3 positionChangeHorizontal;  
    readonly double distance = 0.5;  
 
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        //if (condition)
        //{
            if (this.transform.position.y < 1.5)
            {
                transform.position += (positionChange * Time.deltaTime);
            }

            if (this.transform.position.y >= 1.5)
            {
                transform.position += (positionChangeHorizontal * (Time.deltaTime * 10));
            }
        //}
        
    }
}

The easiest way is probably a spherecast: Unity - Scripting API: Physics.SphereCast

You set it to your current position, set the size to the distance you want to detect the player, then when the spherecast detects something hit, verify its the player (checking for the players script is a good way GetComponent() ).

If it IS the player, then move in the direction you want to move at whatever distance.