- Home /
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
Answer by AnaRhisT · Jun 23, 2010 at 02:55 PM
how to check? while(Vector3.Distance(yourMonsterName.transform,transform.position) > 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;
}
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;
- 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.
Wow thanks, perfect answer.
Little question: What is "your$$anonymous$$onsterName"? A prefab?
I have another little problem
The npc is moving to the waypoint, but it never reach it! Its getting slower and slower and then he just stands there
getting slower means : speed = Random.Range(0.8,1.31); u can make it like speed = Random.Range(10,20); also u can check to see if gets closer with that: if(moveDirection.magnitude
Ive found the error, the cube never reaches the waypoint because the waypoint is too heigh (y-axe).
Is it possible to ignore the heigh?
Answer by clip911 · Jun 15, 2011 at 04:21 AM
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
Your answer
![](https://koobas.hobune.stream/wayback/20220613062829im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
What's causing my NPC Controller to invert? Please Help!! 0 Answers
How do I add NPCs? 2 Answers
NPC Waypoint Choice 0 Answers
Problem with MoveToWaypoint 1 Answer