Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.95 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. using System.Collections;
  4.  
  5. [RequireComponent(typeof(CharacterController))]
  6. public class CreatureMobAI : MonoBehaviour
  7. {
  8.     public int CreatureMobLevel;
  9.     public CreatureType Type;
  10.     public bool isAlive;
  11.     public bool Aggroed;
  12.     public float rotateSpeed = 5;
  13.     public float rotationSpeed = 5.0f;
  14.     public float movementWalkSpeed = 2.0f;
  15.     public float movementRunSpeed = 7.0f;
  16.     Vector3 moveDirection;
  17.     float minAttackDistance = 5.0f;
  18.     Vector3[] pathPoints;
  19.     float wanderDistance = 100;
  20.     float aggroRange = 0;
  21.     Vector3 turnDirection;
  22.     float distanceToPlayer;
  23.     public GameObject player;
  24.     Seeker seeker;
  25.     int i;
  26.     Vector2 rndPoint;
  27.     Vector3 wayPoint;
  28.     Vector3 spawnPoint;
  29.     CreatureAIState CreatureState;
  30.  
  31.     public void Start ()
  32.     {
  33.         //Setup Creature Mob
  34.         if (isAlive == false)
  35.             isAlive = true;
  36.         if (Aggroed == false)
  37.             Aggroed = false;
  38.         if (CreatureMobLevel == 0)
  39.             CreatureMobLevel = 1;
  40.         if (Type == null)
  41.             Type = CreatureType.Neutral;
  42.         if (aggroRange == 0)
  43.             setAggroRange (30);
  44.         if (moveDirection == null)
  45.             moveDirection = Vector3.zero;
  46.         if (spawnPoint == null)
  47.             spawnPoint = transform.position;
  48.         if (player == null)
  49.             player = GameObject.Find ("Player");
  50.         if (seeker == null)
  51.             seeker = GetComponent (typeof(Seeker)) as Seeker;
  52.         CreatureState = CreatureAIState.Wander;
  53.         randomWayPoint ();
  54.         StartCoroutine ("CreatureActions");
  55.     }
  56.  
  57.     private IEnumerator CreatureActions ()
  58.     {
  59.         while (isAlive) {
  60.             switch (CreatureState) {
  61.             case CreatureAIState.Wander:
  62.                
  63.                
  64.                 {
  65.                     Debug.Log ("CreatureState: " + this.name + " Wandering");
  66.                     Wander ();
  67.                     break;
  68.                 }
  69.  
  70.            
  71.             case CreatureAIState.Idle:
  72.                
  73.                
  74.                 {
  75.                     Debug.Log ("CreatureState: " + this.name + " is Idle");
  76.                     Idle ();
  77.                     break;
  78.                 }
  79.  
  80.            
  81.             case CreatureAIState.Attack:
  82.                
  83.                
  84.                 {
  85.                     Debug.Log ("CreatureState: " + this.name + " Attack");
  86.                     Attack ();
  87.                     break;
  88.                 }
  89.  
  90.            
  91.             case CreatureAIState.Engage:
  92.                
  93.                
  94.                 {
  95.                     Debug.Log ("CreatureState: " + this.name + " Engaging");
  96.                     Engage ();
  97.                     break;
  98.                 }
  99.  
  100.            
  101.             case CreatureAIState.Death:
  102.                
  103.                
  104.                 {
  105.                     Debug.Log ("CreatureState: " + this.name + " Died");
  106.                     Death ();
  107.                     break;
  108.                 }
  109.  
  110.            
  111.             case CreatureAIState.Return:
  112.                
  113.                
  114.                 {
  115.                     Debug.Log ("CreatureState: " + this.name + " Returned");
  116.                    
  117.                     break;
  118.                 }
  119.  
  120.             case CreatureAIState.Look:
  121.                
  122.                
  123.                 {
  124.                     Debug.Log ("CreatureState: " + this.name + " Looking Around");
  125.                     Look ();
  126.                     break;
  127.                 }
  128.  
  129.                
  130.             }
  131.             yield return null;
  132.         }
  133.         Death ();
  134.     }
  135.  
  136.     public void Wander ()
  137.     {
  138.         CharacterController controller = GetComponent<CharacterController> ();
  139.         Debug.Log (pathPoints.Length);
  140.        
  141.        
  142.     }
  143.         /*for(i=0;i<pathPoints.Length;){
  144.                 Debug.Log(this.name+": Creature Wandering");
  145.                 GetComponentInChildren<Animation>().animation.Play("Walk");
  146.                        
  147.                 var offset = pathPoints[i] - transform.position;
  148.                        
  149.                 var wantedRotation = Quaternion.LookRotation(offset);
  150.                    
  151.                 transform.rotation = Quaternion.RotateTowards(transform.rotation, wantedRotation, rotateSpeed * Time.deltaTime);
  152.                        
  153.                 moveDirection = transform.TransformDirection(Vector3.forward);
  154.                        
  155.                 moveDirection *= movementWalkSpeed;
  156.            
  157.                 transform.LookAt(pathPoints[i]);
  158.                        
  159.                 controller.Move(moveDirection*Time.deltaTime);
  160.                
  161.                 //if(Vector3.Distance(transform.position,pathPoints[i])<1.0)Debug.Log("Next Point");
  162.             }*/    
  163.    
  164.    
  165.         public void Idle ()
  166.     {
  167.         animation.Play ("Stand1");
  168.     }
  169.  
  170.     public void Attack ()
  171.     {
  172.     }
  173.  
  174.     public void Engage ()
  175.     {
  176.     }
  177.  
  178.     public float getAggroRange ()
  179.     {
  180.         return aggroRange;
  181.     }
  182.  
  183.     public void setAggroRange (float value)
  184.     {
  185.         aggroRange = value;
  186.     }
  187.  
  188.     public void Death ()
  189.     {
  190.         animation.PlayQueued ("Death");
  191.     }
  192.  
  193.     public void Look ()
  194.     {
  195.         randomWayPoint ();
  196.         CreatureState = CreatureAIState.Wander;
  197.     }
  198.  
  199.     public Vector3 randomWayPoint ()
  200.     {
  201.         Debug.Log (this.name + ":" + " Calculating Random Waypoints");
  202.         Vector2 moveOffset = Random.insideUnitCircle * 5;
  203.         wayPoint = new Vector3 (transform.position.x + moveOffset.x, Terrain.activeTerrain.SampleHeight (moveOffset), transform.position.z + moveOffset.y);
  204.         //Debug.Log ("X:" + wayPoint.x + "/" + "Y:" + wayPoint.z + "," + "Height:" + wayPoint.y);
  205.         return wayPoint;
  206.     }
  207.  
  208.     public enum CreatureAIState
  209.     {
  210.         Wander,
  211.         Idle,
  212.         Attack,
  213.         Engage,
  214.         Death,
  215.         Return,
  216.         Look
  217.     }
  218.  
  219.     public enum CreatureType
  220.     {
  221.         Neutral,
  222.         Unknown,
  223.         Aggressive,
  224.         Friendly
  225.     }
  226.  
  227.     public void PathComplete (Vector3[] newPoints)
  228.     {
  229.         pathPoints = newPoints;
  230.     }
  231.  
  232.     public void PathError ()
  233.     {
  234.         Debug.Log ("Pathfinding returned an error!!");
  235.         randomWayPoint ();
  236.     }
  237.  
  238.     public void OnControllerColliderHit (ControllerColliderHit hit)
  239.     {
  240.         randomWayPoint ();
  241.         CreatureState = CreatureAIState.Wander;
  242.     }
  243.  
  244.     public void setPathPoints (Vector3[] points)
  245.     {
  246.         pathPoints = points;
  247.     }
  248.    
  249. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement