error CS1502: The best overloaded method match for `UnityEngine.Vector3.Distance(UnityEngine.Vector3, UnityEngine.Vector3)' has some invalid arguments

I got two of the same errors , I trying to make my enemy ai follow the player . I capitalize the t on transform and I get more errors here is my code :

using UnityEngine;
using System.Collections;

public class Enemyai : MonoBehaviour {
public Transform player;
static Animator anim;
	void Start () 
	{
			anim = GetComponent<Animator> ();
	}
	

	void Update () 
	{
		if (Vector3.Distance(player.position, this.transform) < 10)
		{
			Vector3 direction = player.position - this.transform.position;
			direction.y = 0;
			
			this.transform.rotation = Quaternion.Slerp (this.transform.rotation,Quaternion.LookRotation(direction), 0.1f);
		}
	}
}

3 Answers

3

try to add “.position” to this.transform to get a Vector3 (in line15):

if (Vector3.Distance(player.position, this.transform.position) < 10)

Yeah, the error is because Vector3.Distance has two Vector3 parameters, whereas a transform is not a Vector3 it is a MonoBehaviour class.

Line #15

if (Vector3.Distance(player.position, this.transform) < 10)

should be

if (Vector3.Distance(player.position, this.transform.position) < 10)

Thanks for answer the error went away.

Vector3.Distance takes two Vector3 as arguments.
But this.transform is of type Transform.

use

if (Vector3.Distance(player.position, this.transform.position) < 10) { ... }

instead.