Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Follow/Chase Player

Discussion in 'Scripting' started by JeffAU, Nov 9, 2009.

  1. JeffAU

    JeffAU

    Joined:
    Mar 22, 2009
    Posts:
    387
    Hi all,

    There must be some example scripts in the forum regarding chasing or following a player but I'm having trouble finding them.

    I'm looking for a super simple example of how to get one cube to chase a player cube.

    Any little snippets would be awesome thanks.

    Jeff
     
  2. djoscar

    djoscar

    Joined:
    Jul 31, 2009
    Posts:
    141
    I need this as well!
     
  3. UVRadiation

    UVRadiation

    Joined:
    Jul 21, 2008
    Posts:
    183

    This is a “Line of sight” following :

    Code (csharp):
    1.  
    2. var leader : Transform;
    3. var follower : Transform;
    4. var speed : float =5; // The speed of the follower
    5.  
    6. function Update(){
    7.     follower.LookAt(leader);
    8.     follower.Translate(speed*Vector3.forward*Time.deltaTime);
    9. }
    10.  
    If you want to learn about some more complex chasing\evading behaviors , I highly recommend this book http://oreilly.com/catalog/9780596005559
     
  4. djoscar

    djoscar

    Joined:
    Jul 31, 2009
    Posts:
    141
    Thanks a lot, I'll try this out now.
    P.S. does it matter where I attach this script? To the follower?
     
  5. Zephyrus

    Zephyrus

    Joined:
    Nov 6, 2009
    Posts:
    11
    No, it does not matter. When the script is attached, the Update function will run. You will however have to specify the leader and follower Transforms.
     
  6. UVRadiation

    UVRadiation

    Joined:
    Jul 21, 2008
    Posts:
    183
    If you want to have many followers, use this script :
    Code (csharp):
    1.  
    2. var leader : Transform;
    3. var speed : float =5; // The speed of the follower
    4.  
    5.  
    6. function Update(){
    7.    transform.LookAt(leader);
    8.    transform.Translate(speed*Vector3.forward*Time.deltaTime);
    9. }

    and attach it to the followers. It’s more “Correct” that way
     
  7. JeffAU

    JeffAU

    Joined:
    Mar 22, 2009
    Posts:
    387
    Hey that's great UVRadiation - working great.

    One last question though. I made a cube with the script and created it as a prefab then I instantiate a bunch of these cubes. However I can't get them to follow my First Person Controller.

    When I click on the Cube Prefab I can see the attached script but it wont let me drag the First Person Controller onto the leader variable.

    Any thoughts on how I can instantiate a bunch of cubes all with the same script chasing the same leader?

    Thanks
     
  8. djoscar

    djoscar

    Joined:
    Jul 31, 2009
    Posts:
    141
    Either it's just me or it doesn't work that smoothly :D

    It kind of "follows" the leader but it glitches hell a lot while following and when it reaches the leader even tho I stand still, the follower keeps jumping and spinning around on me :DD

    Anyway thanks for trying ;)
     
  9. Zephyrus

    Zephyrus

    Joined:
    Nov 6, 2009
    Posts:
    11
    Yes, this is a simple script that will look at the leader and move towards it. You will have to do a lot more to make it realistic.

    Try to create an instance of your prefab in the editor by dragging it from the project view. Add the script to the instance, then assign the leader.
    After you've done all that, drag the object (the instance of the prefab) to the prefab in the project view to overwrite the object in there.
    Now you will have a working prefab that "follows the leader".
     
  10. Bastiaens

    Bastiaens

    Joined:
    Oct 8, 2009
    Posts:
    101
    I really like this. and I appreciate the script!
    Now it will come to me, and keeps pushing, so u get a jumpy effect when it reached me (my cube eg.)
    how i change that, and i want it to raise, so above my head. how i do that?
    I think I know, but want a stable script.
    Thanks for answering
     
  11. Grune

    Grune

    Joined:
    Nov 8, 2009
    Posts:
    136
    How about that

    Attach this to the following Object
    and drag the target into the first variable

    Code (csharp):
    1. var player:GameObject;
    2. var dir:Vector3;
    3. var speed:float;
    4. var observed:boolean;
    5.  
    6. function Update () {
    7.    
    8.     if(observed){
    9.     dir = player.transform.position - transform.position;
    10.     dir = dir.normalized;
    11.     transform.Translate(dir * speed, Space.World);
    12.     }  
    13.     else  {
    14.         transform.eulerAngles.y = Mathf.PingPong(Time.time*20,90) -45;
    15.         }
    16. }
    17.    
    18. function OnTriggerEnter(other:Collider) {
    19.     if(player) observed = true;
    20. }
    The Follower will stay and look around - IF you collide with it's collider it will start hunting you :wink: Make the Collider alot bigger, so you can run into it.
    My next stept is to figure out how you could leave its radius and it would stop hunting.
     
  12. Zephyrus

    Zephyrus

    Joined:
    Nov 6, 2009
    Posts:
    11
    Check for the distance to leader in Update. When the distance is above HUNTING_DISTANCE, set observed = false;
     
  13. Grune

    Grune

    Joined:
    Nov 8, 2009
    Posts:
    136
    I understand the priciple, but i don't know how to write a get distance test.
     
  14. UVRadiation

    UVRadiation

    Joined:
    Jul 21, 2008
    Posts:
    183
    Code (csharp):
    1. Vector3.Distance(follower.position,leader.position);
     
  15. Grune

    Grune

    Joined:
    Nov 8, 2009
    Posts:
    136
    cool, thanks
     
  16. Grune

    Grune

    Joined:
    Nov 8, 2009
    Posts:
    136
    na it's not yet working, here's my script:

    Code (csharp):
    1. var leader : Transform;
    2. var follower : Transform;
    3. var speed : float =2;
    4. var range : float =Vector3.Distance(follower.position,leader.position);
    5.  
    6.    
    7. function Update(){
    8.     if (range=<10){
    9.         follower.LookAt(leader);
    10.         follower.Translate(speed*Vector3.forward*Time.deltaTime);
    11.         }
    12.    
    13.     else (){
    14.         speed=0}
    15. }
    I want it to test if i am closer tham 10. The approach me. Else stay put. But it just keeps comming.
     
  17. UVRadiation

    UVRadiation

    Joined:
    Jul 21, 2008
    Posts:
    183
    Thats because :
    Code (csharp):
    1.  
    2. var range : float =Vector3.Distance(follower.position,leader.position);
    3.  
    should be inside the Update function so it will be evaluated every frame.
     
  18. DMJ

    DMJ

    Joined:
    Nov 5, 2009
    Posts:
    83
    If you do it that way, you will calculate the "range" exactly once per object. Instead, you need to recalculate the range in every single Update().

    Code (csharp):
    1.  
    2. // This is the object to follow
    3. var leader : Transform;
    4.  
    5. // This is the object which follows
    6. var follower : Transform;
    7.  
    8. // This is the speed with which the follower will pursue
    9. var speed : float = 2.0;
    10.  
    11. // This is the range at which to pursue
    12. var chaseRange : float = 10.0;
    13.  
    14. // This is used to store the distance between the two objects.
    15. private var range : float;
    16.  
    17.  
    18. function Update(){
    19.  
    20.    // Calculate the distance between the follower and the leader.
    21.    range = Vector3.Distance( follower.position,leader.position );
    22.  
    23.    if ( range <= chaseRange ){
    24.  
    25.       // If the follower is close enough to the leader, then chase!
    26.       follower.LookAt(leader);
    27.       follower.Translate( speed * Vector3.forward * Time.deltaTime);
    28.  
    29.    } // End if (range <= chaseRange)
    30.    
    31.    else {
    32.  
    33.       // The follower is out of range. Do nothing.
    34.       return;
    35.  
    36.    } // End else (if ( range <= chaseRange ))
    37.  
    38. } // End function Update()
    Edit: Yes, I agree with UVRadiation!
     
  19. Grune

    Grune

    Joined:
    Nov 8, 2009
    Posts:
    136
    IT WORKS and even better i understand why

    thanks to both of you, and very good explained
    :D
     
  20. flaminghairball

    flaminghairball

    Joined:
    Jun 12, 2008
    Posts:
    868
    I should think you would be able to just use a FixedJoint, if you're using rigidbodies.
     
  21. Grune

    Grune

    Joined:
    Nov 8, 2009
    Posts:
    136
    the meaning is not to get a fixed chain of riggid bodies. My idea is more like to indirectly control a group of characters. Which do a little bit their own thing but stay in an predefined range :wink:
     
  22. zenislev

    zenislev

    Joined:
    Nov 24, 2009
    Posts:
    20
    Bump: I'm having this same problem. Overwriting the prefab doesn't work; I have the original gameObject following Player(Transform) perfectly, but the prefab still lacks a leader until I manually assign it one when I drag it from the Project view. Any suggestions?
     
  23. trickyspark

    trickyspark

    Joined:
    Apr 24, 2009
    Posts:
    121
    Could you use the waypoint script from the FPS tutorial and maybe attach one of the waypoints to a player? Then Edit the AI script a little maybe?

    Dunno, just a thought.
     
  24. zenislev

    zenislev

    Joined:
    Nov 24, 2009
    Posts:
    20
    Sounds possible. I'd really like to figure out what's going on here though; it's that gnawing feeling that you've made a stupidly obvious mistake but can't see what it might be.

    It seems like the prefab will only accept other prefabs as its leader variable while it's in the Project view, so I guess that's got something to do with it.
     
  25. rmele09

    rmele09

    Joined:
    Nov 8, 2010
    Posts:
    712
    found this very helpful, thanks guys
     
  26. Hajime79

    Hajime79

    Joined:
    Jan 26, 2014
    Posts:
    2
    I know this is an old thread but i'm looking for help on this very subject.

    If i'm using this script for one object to chase another, how do i get the follower to keep constant speed with the target if the target is constantly accelerating?
     
  27. Matheesan

    Matheesan

    Joined:
    Oct 13, 2014
    Posts:
    3
    Hi there, I made game, and I need to add some videos inside the game please if you know can you explain me please, I am very confused.