- Home /
How to create a follow bot. Please help!
I am trying to create a follow bot type character that will follow the player when the player enters a collider. This is what I have so far...
using UnityEngine;
using System.Collections;
public class FollowBot : MonoBehaviour {
Transform Player;
public float AISpeed = 10;
public float MinDistance = 2;
// Use this for initialization
void Start () {
Player = GameObject.FindGameObjectWithTag ("Player").transform;
}
void OnTriggerEnter(Collider obj) {
if(gameObject.tag ==("Player")){
if (Vector3.Distance(transform.position, Player.position) >= MinDistance){
transform.position += transform.forward*AISpeed*Time.deltaTime;
}
}
}
}
It doesn't seem to work at all at the moment but I'm not sure why. It was working previously but when I modified it to make the follow bot follow the player when they enter the collider it stopped working. I can't remember what I changed now because I tried multiple things. If someone could please help me out that would be greatly appreciated. Thank you.
The bot is only going to follow the player when the player enters the trigger, not when he stays in or exits it. How about setting a bool true in the OnTriggerEnter and having in Update() that if the bool is true, do the follow stuff?
Sorry. I'm a bit of a noob and only half understand what you're saying. Could you explain in more detail? Also I want the bot to follow once the player has entered the trigger and should keep following once the player exits.
Untested, but something like this....
using UnityEngine;
using System.Collections;
public class FollowBot : $$anonymous$$onoBehaviour {
Transform Player;
public float AISpeed = 10;
public float $$anonymous$$inDistance = 2;
private bool isFollowing;
// Use this for initialization
void Start () {
Player = GameObject.FindGameObjectWithTag ("Player").transform;
isFollowing = false;
}
void Update()
{
if(isFollowing == true) {
if (Vector3.Distance(transform.position, Player.position) >= $$anonymous$$inDistance){
transform.position += transform.forward*AISpeed*Time.deltaTime;
}
}
}
void OnTriggerEnter(Collider obj) {
if(obj.tag ==("Player")){
isFollowing = true;
}
}
}
This almost worked! The bot moves now, which is a plus. However it just keeps going in a single, forwards direction and doesn't actually follow the player.
after "if (isFollowing == true){ " try transform.LookAt(Player);
your code tells the bot to move forward only, which he will in whatever direction he is facing, so you need to make it face the player.
Answer by GingerNinja14 · Jun 23, 2015 at 10:48 PM
When the player enters the trigger, you are checking that the object that the script is attatched to has the tag Player, not the object entering the trigger. Instead of gameObject.tag, try using obj.tag.
Thank you for your help; however this didn't solve my problem. :(
Answer by TrainWrek · Jun 23, 2015 at 08:37 PM
Here is a simple example:
using UnityEngine;
[RequireComponent(typeof(SphereCollider))]
public class FollowBot : MonoBehaviour {
public bool followingPlayer { get; private set; }
[SerializeField] private float _moveSpeed = 2f;
[SerializeField] private float _minDistance = 2f;
[SerializeField] private float _triggerRadius = 6f;
[SerializeField] private Transform _player;
private Transform _transform;
private void Awake() {
_transform = transform;
//Init player transform
if (_player == null) {
_player = GameObject.FindGameObjectWithTag("Player").transform;
#if UNITY_EDITOR
if (_player == null)
Debug.LogWarning(gameObject.name + "Could not find a Gameobject with the tag \"Player\"");
#endif
}
//Init collider
var collider = GetComponent<SphereCollider>();
collider.radius = _triggerRadius;
collider.isTrigger = true;
//Init rigidbody
var rigidbody = GetComponent<Rigidbody>();
if (rigidbody == null) {
rigidbody = gameObject.AddComponent<Rigidbody>();
rigidbody.isKinematic = true;
rigidbody.useGravity = false;
}
}
private void Update() {
if (followingPlayer && Vector3.Distance(_transform.position, _player.position) > _minDistance) {
Vector3 direction = _player.position - _transform.position;
_transform.position += direction.normalized * _moveSpeed * Time.deltaTime;
_transform.LookAt(_player);
}
}
private void OnTriggerEnter(Collider c) {
if (c.transform == _player)
followingPlayer = true;
}
private void OnTriggerExit(Collider c) {
if (c.transform == _player)
followingPlayer = false;
}
}
Why not use OnTriggerStay ins$$anonymous$$d of dealing with a boolean and making the following behaviour inside the Update function ? I think OnTriggerStay is totally appropriate.
I tried this out but unfortunately it didn't work. Sorry I'm a bit of a noob. Thank you for your help though and if you could offer suggestions to why this didn't work or what I might be doing wrong, that would be greatly appreciated!
Answer by Ibzy · Jun 24, 2015 at 07:06 PM
void OnTriggerEnter(Collider obj) {
if(gameObject.tag ==("Player")){
if (Vector3.Distance(transform.position, Player.position) >= MinDistance){
transform.position += transform.forward*AISpeed*Time.deltaTime;
}
}
}
Here you are checking if gameObject.tag=="Player", you want if(obj.gameObject.tag
And I think obj.gameObject.CompareTag("Player") is meant to be mroe efficient?
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How do I let a camera follow on one axis? 3 Answers
still have some problems with c# AI 1 Answer