Question by
Dog-Gamer · May 12, 2016 at 11:32 PM ·
scripting problemprogramming
Moving script wont even move enemy no more?
I add the code in the //Start and the //End part else the rest is made by unity, i added this code to stop the enemy from laying on his face. code: var speed:float = 5; var directionChangeInterval:float = 1; var maxHeadingChange:float = 30; var target : Transform;
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 (){
//Start
var step = speed * Time.deltaTime;
transform.position =Vector3.MoveTowards(transform.position, target.position, step);
//End
var controller : CharacterController = GetComponent(CharacterController);
transform.eulerAngles = Vector3.Slerp(transform.eulerAngles, targetRotation, Time.deltaTime * directionChangeInterval);
var forward = transform.TransformDirection(Vector3.forward);
controller.SimpleMove(forward * speed);
//Setting the position back to set rotation so it stands up
}
/// <summary>
/// Repeatedly calculates a new direction to move towards.
/// Use this instead of MonoBehaviour.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 = Mathf.Clamp(heading - maxHeadingChange, 0, 360);
var ceil = Mathf.Clamp(heading + maxHeadingChange, 0, 360);
heading = Random.Range(floor, ceil);
targetRotation = new Vector3(0, heading, 0);
}
Comment