Advertisement
Guest User

MinionScript

a guest
May 17th, 2017
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.45 KB | None | 0 0
  1. /// <summary>
  2. /// AI character minion controller.
  3. /// Just A basic AI Character controller
  4. /// will looking for a Target and moving to and Attacking
  5. /// </summary>
  6.  
  7. using UnityEngine;
  8. using System.Collections;
  9. using System.Collections.Generic;
  10. using UnityStandardAssets.Utility;
  11.  
  12. public class MinionsScript : uLink.MonoBehaviour {
  13.  
  14. public UnityEngine.AI.NavMeshAgent agent { get; private set; } // the navmesh agent required for the path finding
  15.  
  16. public float updateTargetRate = 10F;
  17. [HideInInspector] public GameObject targetObj;
  18. public GameObject trigger;
  19. public GameObject healthCanvas;
  20.  
  21. public enum State
  22. {
  23. idle,
  24.  
  25. chasing,
  26.  
  27. attackingMelee,
  28.  
  29. attackingRangue,
  30.  
  31. retreat,
  32.  
  33. dead
  34.  
  35.  
  36. }
  37.  
  38. public SkinnedMeshRenderer skin;
  39. public float attackSpeed = 5;
  40. private float timeAttack = 10;
  41.  
  42. public State creepStatus = State.idle;
  43. private Animator animator;
  44.  
  45.  
  46. public float distanceFromPlayer;
  47. public float maxDistance = 6;
  48.  
  49. //CHILD SCRIPT CREEP HEALTH
  50. private PlayerStats playerStatScript;
  51.  
  52. //CALCULATE POSITION AND ROTATIONS
  53. private float distanceFromOrigin;
  54. private Vector3 objectPos;
  55. private Vector3 initialPos;
  56.  
  57. //AUDIO CLIPS
  58. public AudioClip attackingSound;
  59.  
  60.  
  61. //agent variables
  62. private Vector3 move;
  63. private float m_MovingTurnSpeed = 360;
  64. private float m_StationaryTurnSpeed = 1000;
  65. float m_TurnAmount;
  66. float m_ForwardAmount;
  67.  
  68.  
  69. private WaypointProgressTracker waypointsScript;
  70. private Transform targetPath;
  71.  
  72. void Awake ()
  73. {
  74.  
  75. agent = GetComponentInChildren<UnityEngine.AI.NavMeshAgent>();
  76. agent.updateRotation = false;
  77. agent.updatePosition = true;
  78.  
  79. }
  80.  
  81. void Start () {
  82.  
  83. animator = GetComponent<Animator>();
  84.  
  85. playerStatScript = GetComponent<PlayerStats>();
  86.  
  87. waypointsScript = GetComponent<WaypointProgressTracker>();
  88.  
  89. initialPos = transform.position;
  90.  
  91. }
  92.  
  93.  
  94. void OnTriggerStay(Collider other)
  95. {
  96.  
  97.  
  98. if (playerStatScript.PlayerTeam == "boss" && !targetObj)
  99. {
  100. if(other.tag == "RedPlayerTag" || other.tag == "BluePlayerTag")
  101. {
  102. if (other.GetComponent<PlayerStats>().Health >0)
  103. {
  104. targetObj = other.gameObject;
  105. }
  106. }
  107. }
  108.  
  109. if (playerStatScript.PlayerTeam == "blue" && !targetObj)
  110. {
  111. if(other.tag == "RedPlayerTag" || other.tag == "RedTeamTag")
  112. {
  113. if (other.GetComponent<PlayerStats>().Health >0)
  114. {
  115. targetObj = other.gameObject;
  116. }
  117. }
  118.  
  119. }
  120.  
  121. if (playerStatScript.PlayerTeam == "red" && !targetObj)
  122. {
  123. if(other.tag == "BluePlayerTag" || other.tag == "BlueTeamTag")
  124. {
  125. if (other.GetComponent<PlayerStats>().Health >0)
  126. {
  127. targetObj = other.gameObject;
  128. }
  129. }
  130. }
  131. }
  132.  
  133.  
  134. void Update ()
  135. {
  136. //If the game has ended do nothing!
  137. if (ScoreTable.instance.GameEnded == true)
  138. {
  139. animator.Stop();
  140. return;
  141. }
  142.  
  143. //Debug.Log(distanceFromPlayer);
  144. if (playerStatScript.abilityLocked && playerStatScript.Health > 0)
  145. {
  146. agent.Stop();
  147. creepStatus = State.idle;
  148. animator.enabled = false;
  149. return;
  150. }
  151. else
  152. {
  153. animator.enabled = true;
  154. }
  155.  
  156. if (!playerStatScript.charLocked)
  157. {
  158. targetPath = waypointsScript.target;
  159. timeAttack += Time.deltaTime;
  160. var myPos = this.gameObject.transform.position;
  161. distanceFromOrigin = Vector3.Distance(initialPos,myPos);
  162.  
  163. if(targetObj)
  164. {
  165.  
  166. objectPos = targetObj.transform.position;
  167. objectPos.y = myPos.y;
  168. distanceFromPlayer = Vector3.Distance(objectPos,myPos);
  169.  
  170. if (targetObj.GetComponent<PlayerStats>().Health <=0)
  171. {
  172. targetObj = null;
  173. creepStatus = State.idle;
  174. return;
  175. }
  176.  
  177. }
  178. else
  179. {
  180. creepStatus = State.idle;
  181. }
  182.  
  183. switch (creepStatus)
  184. {
  185. case State.idle:
  186.  
  187. if (playerStatScript.Health <= 0)
  188. {
  189. creepStatus = State.dead;
  190. return;
  191. }
  192.  
  193. animator.SetInteger("AnimType", (int)State.chasing);
  194. MoveOrChase();
  195.  
  196. if(distanceFromPlayer<=1.5f && targetObj) {
  197. initialPos = transform.position;
  198. creepStatus = State.attackingMelee;
  199. }
  200.  
  201.  
  202. break;
  203. case State.dead:
  204.  
  205.  
  206. animator.SetInteger("AnimType", (int)State.dead);
  207.  
  208. break;
  209. case State.attackingMelee:
  210. if (playerStatScript.Health <= 0)
  211. {
  212. creepStatus = State.dead;
  213. return;
  214. }
  215.  
  216. if(distanceFromPlayer<=1.5f) {
  217. if (timeAttack > attackSpeed)
  218. {
  219. transform.LookAt(targetObj.transform.position);
  220. agent.Stop();
  221. animator.SetInteger("AnimType", (int)State.attackingMelee);
  222. playerStatScript.charLocked = true;
  223. timeAttack = 0;
  224. }
  225. else
  226. {
  227. transform.LookAt(targetObj.transform.position);
  228. agent.Stop();
  229. animator.SetInteger("AnimType", (int)State.idle);
  230. }
  231. }
  232. else
  233. {
  234. animator.SetInteger("AnimType", (int)State.chasing);
  235.  
  236. MoveOrChase();
  237. }
  238.  
  239. if(distanceFromPlayer>=maxDistance || distanceFromOrigin > maxDistance)
  240. {
  241. targetObj = null;
  242. creepStatus = State.idle;
  243. return;
  244. }
  245.  
  246. break;
  247.  
  248. case State.retreat:
  249. targetObj = null;
  250. creepStatus = State.idle;
  251.  
  252. break;
  253. }
  254. }
  255. }
  256.  
  257.  
  258. void MoveOrChase()
  259. {
  260. updateTargetRate++;
  261. if (updateTargetRate > 5)
  262. {
  263. if (targetObj != null)
  264. {
  265. updateTargetRate = 0;
  266. //Vector3 tmpDirection = transform.position - targetObj.transform.position;
  267. //tmpDirection.Normalize();
  268. //Vector3 tmpPosition = targetObj.transform.position + tmpDirection;
  269. agent.SetDestination(targetObj.transform.position);
  270. }
  271. else
  272. {
  273. updateTargetRate = 0;
  274. agent.SetDestination(targetPath.position);
  275. }
  276. }
  277.  
  278. agent.Resume();
  279.  
  280.  
  281. move = agent.desiredVelocity;
  282. if (move.magnitude > 1f) move.Normalize();
  283. move = transform.InverseTransformDirection(move);
  284. move = Vector3.ProjectOnPlane(move, Vector3.zero);
  285. m_TurnAmount = Mathf.Atan2(move.x, move.z);
  286. m_ForwardAmount = move.z;
  287. float turnSpeed = Mathf.Lerp(m_StationaryTurnSpeed, m_MovingTurnSpeed, m_ForwardAmount);
  288. transform.Rotate(0, m_TurnAmount * turnSpeed * Time.deltaTime, 0);
  289. transform.LookAt(agent.nextPosition);
  290.  
  291.  
  292. }
  293. //METHOD CALLED FROM THE ANIMATION EVENTS TO UNLOCK THE CHARACTER
  294. void ExitAnim()
  295. {
  296. playerStatScript.charLocked = false;
  297.  
  298. }
  299.  
  300.  
  301. void Attacking(State type) {
  302.  
  303. switch (type)
  304. {
  305. case State.attackingMelee:
  306.  
  307.  
  308. creepStatus = State.attackingMelee;
  309.  
  310. break;
  311. }
  312.  
  313. if (targetObj)
  314. {
  315.  
  316. PlayerStats enemyPlayerStatsScript = targetObj.GetComponent<PlayerStats>();
  317.  
  318. enemyPlayerStatsScript.DrainHealth(playerStatScript.BAdValue, playerStatScript.BApValue, transform.name, this.gameObject, gameObject.tag, CharacterClass.charName.Empty);
  319.  
  320. if (enemyPlayerStatsScript.Health <= 0)
  321. {
  322. targetObj = null;
  323. creepStatus = State.idle;
  324. }
  325.  
  326. }
  327. else
  328. {
  329. creepStatus = State.idle;
  330. }
  331. }
  332.  
  333. [RPC]
  334. public void Respawn ()
  335. {
  336.  
  337. }
  338.  
  339.  
  340. [RPC]
  341. public IEnumerator ImDead()
  342. {
  343. creepStatus = State.dead;
  344. trigger.GetComponent<BoxCollider>().enabled = false;
  345. agent.enabled = false;
  346. animator.SetInteger("AnimType", (int)State.dead);
  347.  
  348. yield return new WaitForSeconds(playerStatScript.RespawnTime);
  349. targetObj = null;
  350. Destroy(targetPath.gameObject);
  351. gameObject.SetActive(false);
  352.  
  353. }
  354.  
  355. private void OnEnable()
  356. {
  357. agent.enabled = true;
  358. trigger.GetComponent<BoxCollider>().enabled = true;
  359. networkView.RPC("EnableOnClient", uLink.RPCMode.Others);
  360. }
  361.  
  362.  
  363.  
  364. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement