I made two scripts. 1 for the player and 1 for the AI. The player is supposed to look at the AI and that would cause the AI to move towards the player. When I test it out, it doesn’t work.
Player Script
{
Ray thugRay;
RaycastHit rayHit;
public int dist = 20;
void Update()
{
thugRay = new Ray(transform.position, transform.forward * dist);
Debug.DrawRay(transform.position, transform.forward * dist, Color.red);
if (Physics.Raycast(transform.position, transform.forward, out rayHit, dist))
{
{
if (rayHit.transform.gameObject.tag == ("It"))
{
Debug.Log("He looked at you");
GameObject.Find("Control").GetComponent("Move").enabled = false;
}
}
}
}
}
AI Script
{
public Transform player;
public float walkingDistance = 10.0f;
public float smoothTime = 10.0f;
private Vector3 smoothVelocity = Vector3.zero;
RaycastHit rayHit;
private void Start()
{
}
void HitByRay()
{
Debug.Log("Rip");
transform.LookAt(player);
float distance = Vector3.Distance(transform.position, player.position);
if (distance < walkingDistance)
{
transform.position = Vector3.SmoothDamp(transform.position, player.position, ref smoothVelocity, smoothTime);
}
rayHit.transform.SendMessage("HitByRay");
}
}