- Home /
 
Question is off-topic or not relevant. Also a duplicate and asking for code!
Monster AI chase and collider
Hey all! I recently got this script to make my monster in my horror game wander, but it keeps walking into walls. I want to make it so when it touches a wall, it turns and moves. I also want it to when a player gets close, it attacks and chases them. I am new to Unity so if anyone has anything helpful please say so thanks!
pragma strict
 /// <summary>
 /// Creates wandering behaviour for a CharacterController.
 /// </summary>
 @script RequireComponent(CharacterController)
  
     var speed:float = 5;
     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.SimpleMove(forward * speed);
     }
  
     /// <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);
     }
 
              For the walls, you'll need to figure out how this script will know what a "wall" is. Are you Raycasting or using colliders/triggers on your walls?
I think colliders... I'm very new to Unity and I'm only 14. The wall has a box collider and I have no idea what ray casting is to be honest
Follow this Question
Related Questions
Vector3.Slerp bumping problem 1 Answer
How can I get the position of Input.touches in World Space? 1 Answer
Vector3.Slerp is not working help. 2 Answers
Make Object smoothly follow player if out of near range 1 Answer
My character isn't moving 0 Answers