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);
}
}
}
Answer by QuiZr · Jul 17, 2016 at 03:06 PM
Line #15
if (Vector3.Distance(player.position, this.transform) < 10)
should be
if (Vector3.Distance(player.position, this.transform.position) < 10)
Answer by allan36j · Jul 17, 2016 at 12:46 PM
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 $$anonymous$$onoBehaviour class.
Transform does not inherit from $$anonymous$$onoBehaviour. Just Component
@importguru88 could you or a moderator please "accept" my answer if it was what you were looking for.
Answer by tststs · Jul 17, 2016 at 03:07 PM
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.