- Home /
How i make the NPC friend stop following me and start to follow the enemy when he is near???!!!
I am developing a game based on Solo Leveling, and my compannion ("shadows") is following me and i cannot find a way to make them start to follow the enemy when he is near. Does anyone knows how to solve my problem?
Here is the code:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class ShadowFollow : MonoBehaviour {
public Transform target;
public int moveSpeed = 5;
public int rotationSpeed = 2;
public Transform myTransform;
public float minDistance = 7f;
public float maxDistance = 15f;
void Awake()
{
myTransform = transform;
}
void Start()
{
target = GameObject.FindWithTag("Player").transform;
}
void Update()
{
Vector3 Distance = target.position - myTransform.position;
if (Distance.sqrMagnitude > minDistance * minDistance)
{
myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed * moveSpeed * Time.deltaTime);
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
}
}
}
Comment
I had tried to use Bool to check if the enmy is near, i dont have so muvh experience to try this alone
Your answer
Follow this Question
Related Questions
Help with Enemy AI 1 Answer
How To Make Enemy Move Around 0 Answers
Problem with a monster (he follows me backwards) 1 Answer
Enemy Pathing 1 Answer
Need help with enemy AI 1 Answer