Hello,
If player leaves from sphere radius, monster will reach last distance where player leaves and he will stay in his position.
Can you guys give me some advice what I need to do ?
There is my script :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
[RequireComponent(typeof(SphereCollider))]
public class Monster : MonoBehaviour {
GameObject player;
bool playerInSphere;
public float distance = 1.0f;
public Transform target;
public float speed = 3f;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
if (playerInSphere == true)
{
MoveToPlayer ();
}
}
void OnTriggerEnter (Collider other)
{
if (other.gameObject.tag == "Player")
{
Debug.Log ("He entered");
playerInSphere = true;
}
}
void OnTriggerExit (Collider other)
{
if (other.gameObject.tag == "Player")
{
StopMovement ();
Debug.Log ("He leaves");
}
}
public void MoveToPlayer ()
{
//rotate to look at player
transform.LookAt (target.position);
transform.Rotate (new Vector3 (0, -90, 0), Space.Self);
//move towards player
if (Vector3.Distance (transform.position, target.position) > distance)
{
transform.Translate (new Vector3 (speed * Time.deltaTime, 0, 0));
}
}
public void StopMovement ()
{
}
}