- Home /
This question was
closed Apr 08, 2013 at 07:18 AM by
AlucardJay for the following reason:
Other : question has been answered before, and can be found by searching
How To Make Enemy Move Around
Hello Everyone i use SpawnScript to spawn enemy
and when i get close enemy, he try to follow me
but the problem that if he didn't saw me, he keep stand in his position
i want him to move around
if(distance > lookAtDistance)
{
//MOVE ARROUND
}
i just tried very simple code :
transform.position = Vector3.Lerp(transform.position, transform.position + new Vector3(1,0,1), 0.2f);
but he keep moving forward until he fall from the terrain xD
i need him to move around his range, almost like circle way
Comment
#pragma strict
/// <summary>
/// Creates wandering behaviour for a CharacterController.
/// </summary>
@script RequireComponent(CharacterController)
var speed:float = 1;
var directionChangeInterval:float = 1;
var maxHeadingChange:float = 30;
var heading: float=0;
var targetRotation: Vector3 ;
function Awake (){
// Set random initial rotation
transform.eulerAngles = Vector3(0, Random.Range(0,360), 0); // look in a random direction at start of frame.
//StartCoroutine
NewHeadingRoutine ();
}
function Update (){
var controller : CharacterController = GetComponent(CharacterController);
transform.eulerAngles = Vector3.Slerp(transform.eulerAngles, targetRotation, Time.deltaTime * directionChangeInterval);
var forward = transform.TransformDirection(Vector3.forward);
controller.Simple$$anonymous$$ove(forward * speed);
}
/// <summary>
/// Repeatedly calculates a new direction to move towards.
/// Use this ins$$anonymous$$d of $$anonymous$$onoBehaviour.InvokeRepeating so that the interval can be changed at runtime.
/// </summary>
while (true){
NewHeadingRoutine();
yield WaitForSeconds(directionChangeInterval);
}
/// <summary>
/// Calculates a new direction to move towards.
/// </summary>
function NewHeadingRoutine (){
var floor = $$anonymous$$athf.Clamp(heading - maxHeadingChange, 0, 360);
var ceil = $$anonymous$$athf.Clamp(heading + maxHeadingChange, 0, 360);
heading = Random.Range(floor, ceil);
targetRotation = new Vector3(0, heading, 0);
}
try this and ask if you have problem
Follow this Question
Related Questions
Monster doesn't move 0 Answers
Move Enemy randomly in a range. 3D 0 Answers
Enemy not rotating when inside range 1 Answer
Enemy following the target with ITween 2 Answers
Enemy Follow Script Help 2 Answers