Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections.Generic;
- using System.Collections;
- [RequireComponent(typeof(CharacterController))]
- public class CreatureMobAI : MonoBehaviour
- {
- public int CreatureMobLevel;
- public CreatureType Type;
- public bool isAlive;
- public bool Aggroed;
- public float rotateSpeed = 5;
- public float rotationSpeed = 5.0f;
- public float movementWalkSpeed = 2.0f;
- public float movementRunSpeed = 7.0f;
- Vector3 moveDirection;
- float minAttackDistance = 5.0f;
- Vector3[] pathPoints;
- float wanderDistance = 100;
- float aggroRange = 0;
- Vector3 turnDirection;
- float distanceToPlayer;
- public GameObject player;
- Seeker seeker;
- int i;
- Vector2 rndPoint;
- Vector3 wayPoint;
- Vector3 spawnPoint;
- CreatureAIState CreatureState;
- public void Start ()
- {
- //Setup Creature Mob
- if (isAlive == false)
- isAlive = true;
- if (Aggroed == false)
- Aggroed = false;
- if (CreatureMobLevel == 0)
- CreatureMobLevel = 1;
- if (Type == null)
- Type = CreatureType.Neutral;
- if (aggroRange == 0)
- setAggroRange (30);
- if (moveDirection == null)
- moveDirection = Vector3.zero;
- if (spawnPoint == null)
- spawnPoint = transform.position;
- if (player == null)
- player = GameObject.Find ("Player");
- if (seeker == null)
- seeker = GetComponent (typeof(Seeker)) as Seeker;
- CreatureState = CreatureAIState.Wander;
- randomWayPoint ();
- StartCoroutine ("CreatureActions");
- }
- private IEnumerator CreatureActions ()
- {
- while (isAlive) {
- switch (CreatureState) {
- case CreatureAIState.Wander:
- {
- Debug.Log ("CreatureState: " + this.name + " Wandering");
- Wander ();
- break;
- }
- case CreatureAIState.Idle:
- {
- Debug.Log ("CreatureState: " + this.name + " is Idle");
- Idle ();
- break;
- }
- case CreatureAIState.Attack:
- {
- Debug.Log ("CreatureState: " + this.name + " Attack");
- Attack ();
- break;
- }
- case CreatureAIState.Engage:
- {
- Debug.Log ("CreatureState: " + this.name + " Engaging");
- Engage ();
- break;
- }
- case CreatureAIState.Death:
- {
- Debug.Log ("CreatureState: " + this.name + " Died");
- Death ();
- break;
- }
- case CreatureAIState.Return:
- {
- Debug.Log ("CreatureState: " + this.name + " Returned");
- break;
- }
- case CreatureAIState.Look:
- {
- Debug.Log ("CreatureState: " + this.name + " Looking Around");
- Look ();
- break;
- }
- }
- yield return null;
- }
- Death ();
- }
- public void Wander ()
- {
- CharacterController controller = GetComponent<CharacterController> ();
- Debug.Log (pathPoints.Length);
- }
- /*for(i=0;i<pathPoints.Length;){
- Debug.Log(this.name+": Creature Wandering");
- GetComponentInChildren<Animation>().animation.Play("Walk");
- var offset = pathPoints[i] - transform.position;
- var wantedRotation = Quaternion.LookRotation(offset);
- transform.rotation = Quaternion.RotateTowards(transform.rotation, wantedRotation, rotateSpeed * Time.deltaTime);
- moveDirection = transform.TransformDirection(Vector3.forward);
- moveDirection *= movementWalkSpeed;
- transform.LookAt(pathPoints[i]);
- controller.Move(moveDirection*Time.deltaTime);
- //if(Vector3.Distance(transform.position,pathPoints[i])<1.0)Debug.Log("Next Point");
- }*/
- public void Idle ()
- {
- animation.Play ("Stand1");
- }
- public void Attack ()
- {
- }
- public void Engage ()
- {
- }
- public float getAggroRange ()
- {
- return aggroRange;
- }
- public void setAggroRange (float value)
- {
- aggroRange = value;
- }
- public void Death ()
- {
- animation.PlayQueued ("Death");
- }
- public void Look ()
- {
- randomWayPoint ();
- CreatureState = CreatureAIState.Wander;
- }
- public Vector3 randomWayPoint ()
- {
- Debug.Log (this.name + ":" + " Calculating Random Waypoints");
- Vector2 moveOffset = Random.insideUnitCircle * 5;
- wayPoint = new Vector3 (transform.position.x + moveOffset.x, Terrain.activeTerrain.SampleHeight (moveOffset), transform.position.z + moveOffset.y);
- //Debug.Log ("X:" + wayPoint.x + "/" + "Y:" + wayPoint.z + "," + "Height:" + wayPoint.y);
- return wayPoint;
- }
- public enum CreatureAIState
- {
- Wander,
- Idle,
- Attack,
- Engage,
- Death,
- Return,
- Look
- }
- public enum CreatureType
- {
- Neutral,
- Unknown,
- Aggressive,
- Friendly
- }
- public void PathComplete (Vector3[] newPoints)
- {
- pathPoints = newPoints;
- }
- public void PathError ()
- {
- Debug.Log ("Pathfinding returned an error!!");
- randomWayPoint ();
- }
- public void OnControllerColliderHit (ControllerColliderHit hit)
- {
- randomWayPoint ();
- CreatureState = CreatureAIState.Wander;
- }
- public void setPathPoints (Vector3[] points)
- {
- pathPoints = points;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement