- Home /
Help with a chasing script
I am trying to get an enemy to chase a vehicle and get close enough to collide. All the other scripts and questions all have just following and stopping at a certain distance I can't seem to find one that just follows and even if the player stops it keeps going and rams into the player. Please help and if possible could you explain what exactly is happening so I can know why certain things I was trying weren't working. THANK YOU!
If your problem is solved please accept an answer. If not please give an update on what changed.
Answer by Fornoreason1000 · May 01, 2013 at 07:24 PM
ok... you've requested for something simpler than what you have found lol. all you need to is use RotateTowards and MoveTowards to get started you can make it fancier as you get confident. of course you want the enemy to be moving towards the player. so you will need to reference the player somehow. you can do this by using FindObjectsWithTag, then casting it as a GameObject. from there you can get the transform(vector3) you need.
http://docs.unity3d.com/Documentation/ScriptReference/GameObject.FindWithTag.html http://docs.unity3d.com/Documentation/ScriptReference/Vector3.RotateTowards.html http://docs.unity3d.com/Documentation/ScriptReference/Vector3.MoveTowards.html
good luck, besure to check Burzerg's tutorial on this
none of that helped me I watched both of the AI video tutorials and tried everything that I could think of and still just got it where it follows and as soon as it got to a certain range stopped and if I went backwards it rotated around the car and appeared in front of the player.
Please don't post comments as Answers - I've converted them for you this time.
it shouldn't be stopping until it ends up on you exact position(inside of you). it sounds like you have a child object off center to a parent one and referencing the parent making the object stop because it on the parent but not the child. or you doing a distance code without realizing. or you have a another collider in the way.
Answer by ExTheSea · May 01, 2013 at 07:21 PM
I just quickly searched unity chasing script in google and found this: http://answers.unity3d.com/questions/274809/how-to-make-enemy-chase-player-basic-ai.html
Just use this and set MinDist to 0 or just remove that entire if(....
Another thread which i found as well is this: http://forum.unity3d.com/threads/34046-Follow-Chase-Player if the answer for some reason is not enough.
Hope this helps
I tried using all those examples before ExTheSea and modified them to what I thought would work and it still wouldn't work the way I need them too and the Follow Chase Player script from the Forum won't work because it is using a follower using that will never the two collide. I also tried all three of Fornoreason1000's suggestions and still didn't get what I needed when I tried coding the 3 links in my code the two game objects never got close enough to collide the chasing one just got to a certain distance then stopped and if i were to back up it would rotate around the player and start following him in reverse as the player was reversing, I even watched both the video tutorials on AI like you suggested and that didn't get me anywhere.
Are you sure the one who chases the player doesn't stop because they hit the collider of the player or the collider of the follower?
Also make sure that no other script is interfering with the FollowChaseScript and makes it stop before it gets too close to the player.
I tried the FollowChaseScript from the forum and it worked for me. I made a cube with a rigidbody+box collider as the follower and the First Person controller as the leader. The cube caught up to the player and made the player pushed around the place.
ExTheSea it does follow and register that I have hit my player character but now using that forum code my enemy floats slightly in the air and blinks back and forth from in front and behind the player at a certain distance away and I do not have a box collider on my player and the box collider i have on my enemy is tight against the cubes that are making the front of the enemy. And before I get scolded again I would comment this but there is no CO$$anonymous$$$$anonymous$$ENT BUTTON like the tutorial video says there should be.
Is there really no button like this:
Btw.: Behold my amazing arrow-painting-Skills :)
I'm starting to think that the chasing script works correctly and your problem just come from the situation when the follower collider hits the Character Controller/Collider of the player.
Although the thing with the cube switching between being in the front and in the back sounds a bit weird i observed similar weird things when i tried the chase-Script myself but that's just unity physics in such a situation. This maybe can be fixed by playing around with the chase speed, the mass of the follower/player, ..... or you change your script so that at a specific distance the follower will gets quicker and when he rams the player (Collision is triggered) his speed gets reduced for a few seconds so that he falls back a bit again.
Just an idea which came to my $$anonymous$$d.
Your answer
![](https://koobas.hobune.stream/wayback/20220613101346im_/https://answers.unity.com/themes/thub/images/avi.jpg)