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!

1 Answer

1

I have made videos on exactly what you are asking. Your script is exactly like at video 23 :

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 : Alucard Jay - YouTube

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

Thanks so much ! I am gonna watch all your videos!

@alucardj, I just read the forum thread on programming 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!