- Home /
Help! How do I prevent a follower from turning heel and running away?
I have a few characters I'm trying to get to follow each other in a chain - dynamically. I have the follow script itself working, but it's exhibiting odd behavior. If the followers get too close to the main character in the chair, it'll circle and veer around, then dash off in the opposite direction never to return. How can I best prevent that? I "THINK" it could be prevented by slowing the follower if they get too close until the player is further away again.
using UnityEngine; using System.Collections;
public class Duckling : MonoBehaviour {
GameObject player;
CharacterController motor;
float basicSpeed = 6.5f;
Vector3 moveForward;
float playerMagnitude;
public int minRange;
public bool follow;
public float moveSpeed;
public float rotationSpeed;
// Use this for initialization
void Start () {
player = GameObject.FindGameObjectWithTag("Player");
motor = GetComponent();
}
// Update is called once per frame
void Update () {
//moveForward = transform.forward * basicSpeed * Time.deltaTime;
playerMagnitude = (player.transform.position - transform.position).magnitude;
Debug.Log (playerMagnitude);
followPlayer();
}
void followPlayer()
{
if (playerMagnitude < 4)
{
moveForward = transform.forward * basicSpeed * Time.deltaTime;
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(player.transform.position - transform.position), 3 * Time.deltaTime);
motor.Move(moveForward);
}
}
}
Your answer
Follow this Question
Related Questions
Enemy prefab with NavMeshAgent change speed in other map? 0 Answers
Enemy following the target with ITween 2 Answers
Speed based on Distance 3 Answers
Make Clone AIs Stay Apart? 1 Answer
ai pathfinding project getting started scene problems 1 Answer