I'm trying to create a simple EnemyAI where the enemy follows the player. I have problem where the Engine cannot convert 'UnityEngine.GameObject' to 'UnityEngine.Transform'. Here's the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyAI : MonoBehaviour
{
Transform TargetedPlayer;
public float EnemySpeed = 3f;
void FixedUpdate()
{
EnemyFollow();
}
void EnemyFollow()
{
TargetedPlayer = GameObject.Find("Pig"); //Issue
if (Vector3.Distance(transform.position,TargetedPlayer.position)>1f)
{
transform.Translate(new Vector3(EnemySpeed * Time.deltaTime,0,0) );
}
}
}
I understand the problem but I don't know how to change the code without breaking it. Thanks In Advance to the people that helped :)
CodePudding user response:
I'm taking a wild guess here but make
[SerializeField] Transform TargetedPlayer;
or
public Transform TargetedPlayer;
and drag the player there
and delete
TargetedPlayer = GameObject.Find("Pig"); //Issue
In my little experience I would recommend you if you can, use NavMeshAgent for the enemy to follow the player