Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by LethalBeauty23 · Jun 23, 2015 at 06:59 PM · c#followfollow player

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.

Comment
Add comment · Show 6
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MajorParts · Jun 23, 2015 at 07:25 PM 1
Share

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?

avatar image LethalBeauty23 · Jun 24, 2015 at 04:48 PM 0
Share

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.

avatar image MajorParts · Jun 24, 2015 at 06:11 PM 1
Share

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;
              }
          }
      }
      
avatar image LethalBeauty23 · Jun 24, 2015 at 06:28 PM 0
Share

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.

avatar image MajorParts · Jun 24, 2015 at 06:46 PM 1
Share

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.

Show more comments

3 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image LethalBeauty23 · Jun 24, 2015 at 04:48 PM 0
Share

Thank you for your help; however this didn't solve my problem. :(

avatar image
0

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;
     }
 }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Hellium · Jun 23, 2015 at 10:53 PM 0
Share

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.

avatar image LethalBeauty23 · Jun 24, 2015 at 04:50 PM 0
Share

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!

avatar image
0

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?

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

25 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Set limits for camera to other axis 2D 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges