- Home /
Need AI script that makes object follow the player (and collidors work)
I have this code till now,
var target : Transform; //the enemy's target
var moveSpeed = 3; //move speed
var rotationSpeed = 3; //speed of turning
var myTransform : Transform; //current transform data of this enemy
function Awake()
{
myTransform = transform; //cache transform data for easy access/preformance
}
function Start()
{
target = GameObject.FindWithTag("Player").transform; //target the player
}
function Update () {
//rotate to look at the player
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
//move towards the player
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
}
I think you answered my question too, thanks :) If you go and post an answer ill up vote :)
http://answers.unity3d.com/questions/423222/question-about-enemy-ai.html
Thanks so much! I was just about to ask this question!
Answer by AlucardJay · Mar 23, 2013 at 06:55 PM
I have made videos on exactly what you are asking. Your script is exactly like at video 23 :
23 _ NPC 1-1 : Moving : http://youtu.be/p9yLZmgkFWY
24 _ NPC 1-2 : Colliding : http://youtu.be/7di6FnR7rqU
25 _ NPC 1-3 : Grounded and Turning : http://youtu.be/78a1VSaaDuo
26 _ Raycasting Basics : http://youtu.be/Sj4oxfQqzhA
27 _ NPC 1-4 : Basic Obstacle Avoidance : http://youtu.be/JlulRhjLsNY
28 _ NPC 1-4a : Basic Obstacle Avoidance : http://youtu.be/szxB6RBhVyQ
And I have just taken a break from recording more videos to see your question. So check out all my videos on Programming and Making Games with Unity 3D : http://www.youtube.com/channel/UCKO90x3pGUgpOjhV-HhWVIw/videos?view=1&flow=grid
If you want some written explanation, here is the answer that I wrote, then is adapted in my rigidbody character controller videos. Don't forget to read the comments about adding the isGrounded variable with the collision events, but it's all in the videos, hope you find them useful : http://answers.unity3d.com/questions/406685/my-enemies-merge-when-they-come-towards-me.html
Hi again $$anonymous$$kSv, as I just posted on youtube : I am really very sorry but there have been too many times where someone just copy-pastes, then hits play, then post a new question here saying 'it doesn't work'.
This is no reflection on you, am really glad that you might find my videos useful. But I cannot post my scripts here anymore. So the few have really ruined it for the many :( I could show you so many many UA questions where this has happened, and it has driven me a little bit mad!
If you are having problems and are really stuck, then you are most welcome to come over to one of my forum pages. There I am very happy to help with any problems you are having. Here are the links to the forum pages :
Again, if there are any problems at all, please just visit on of the forum pages, and I am most happy to answer any question. Am glad you posted this comment, I could give you much more detail than in a youtube comment! All the best, am really happy to have you watching, and hope to see you in the forums =]
@alucardj, I just read the forum thread on program$$anonymous$$g and making games with Unity. You sir, have gain my salute and respect. You have some serious contributions to the community. I will go through all the videos in the forum and learn something new from there.
heh, thanks so much dude, its the comments like yours there that just gives me a big smile and makes my day, awesome. Wow, yes, I am not just complaining about 'the problem' (you know what I mean here from my other UA comments), but am really trying to do something about it. There are just not enough hours in the day!
Your answer
Follow this Question
Related Questions
Why is my AI not thinking the collisions fast enough? 1 Answer
Enemy Ai Problem with the collider and gravity 1 Answer
NavmeshAgent vs Character Controller = Absolute Unity Failure? 1 Answer
Jittering movement when colliding with object 1 Answer
Have falling object exit from a collider after collision? 2 Answers