Search code examples
c#unity-game-engineunityscript

No overload for method 'Distance' takes 1 arguments


I'm trying to code adversaries to move between two points and destroy the player if touched.

public class MovementBetweenPoints : MonoBehaviour {
    public Transform[] keyPoints;
    public float speed;
    private int currentKeyPoint;
    public float min_Distance;
    public float Distance;

    // Use this for initialization
    void Start () 
    {
        transform.position = keyPoints[0].position;
        currentKeyPoint = 1;
    }

    // Update is called once per frame
    void Update () 
    { 
        // ----------- Error happens on next line
        if (Vector3.Distance(transform.position - keyPoints[currentKeyPoint].position) <= min_Distance)   
        {
            currentKeyPoint++;
        }

        if (currentKeyPoint >= keyPoints.Length)
        {
            currentKeyPoint = 0;
        }

        transform.position = Vector3.MoveTowards(transform.position, keyPoints[currentKeyPoint].position, speed * Time.deltaTime);
    }

    void OnTriggerEnter(Collider Player)
    {
        Destroy(Player.gameObject);
    }

}

No overload for method 'Distance' takes 1 arguments."

How to fix it?


Solution

  • The Distance call returns the distance between two points but the code you wrote only gives one point. I think you have a "-" where you wanted a",".
    Try this:

    if (Vector3.Distance(transform.position, keyPoints[currentKeyPoint].position) <= min_Distance)