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;
}
}
}
↧