How to move a object to my position?

Hello I want an object (Monster/Npc) to rotate to my player and move to me, when I'm too close to it.

So my questions:

  • How to check if I'm close enough to the monster (Aggro Range)
  • How to let the monster move to my position (Walking to me, not porting).
  • How to make the monster walking to specific waypoints?

I know that theres allready a waypoint script, but the problem is, that I cant choose which monster is using which waypoints, instead the monsters use the waypoints that are closest to them.

Thank you very much

  1. how to check? `while(Vector3.Distance(yourMonsterName.transform,transform.position) > 2)`

  2. rotate + move

    var rotationSpeed : float = 0.2;
    var direction : Vector3 = transform.position - yourMonsterName.position;
     while(Vector3.Distance(yourMonsterName.position,transform.position) > 2)
        {
          transform.rotation = Quaternion.Slerp(transform.position,Quaternion.LookAt(direction), rotationSpeed* Time.deltaTime);
        transform.eulerAngles = new Vector3(0,transform.eulerAngles,0);
        yield;
    
    <pre>`}
    
    

    `

it was above how to rotate, now how to move?

   while(Vector3.Distance(yourMonsterName.position, transform.position) > 2){   
            //animation.CrossFade("walk"); //if u have animation @walk
            var controller : CharacterController = GetComponent(CharacterController); // reference the CharacterController to controller
            var direction : Vector3 = transform.position - yourMonsterName.position;

            controller.SimpleMove(direction.normalized * speed);
            yield;

}

Also , I'm not but this: `var direction : Vector3 = transform.position - yourMonsterName.position`; can be changed to this: `var direction : Vector3 = yourMonsterName.position - transform.position;`

  1. add this script to ur enemy (it must have character controller though)

http://09duck.pastebin.com/2DCZXt4M

open the waypoint slot in the monster's inspector and add 3 waypoints gameobjects to it.

so u may ask how to create a way point and recognize it in inspector?

so paint a simple circle in ur Paint program and it to unity's project, open a folder called "Gizmos" it's case sensitive btw! so make it as written in quotation marks,add the picture to it, make sure u saved the picture as jpeg/jpg format! and make sure the picture is called "waypoint"!

add this script to ur waypoint gameobject : http://09duck.pastebin.com/LYpj7aLW so now u'll see ur waypoint with a picture, now u can attach all ur 2,3,4,5,6 or how many u want to ur waypoint. the script is designed to work on 3 waypoint, but u can simple make it 4,5,6,7 by changing this line :

if(currentWaypoint == 3){ currentWaypoint -= 3; }

to like:

if(currentWaypoint == 5){ currentWaypoint -= 5; }

which means 5 waypoints, when it reaches the 5th it goes to the first one.

i got another question.
i want to move an object e.g like a cube , randomly into a limited space like
position.x = Random.Range(-4,4)
position.y = Random.Range(-4,4)
but as we know position does not translate the object its just snap to the position