Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

What goes into "Physic.Simulate" in the Profiler?

Discussion in 'Scripting' started by infinitypbr, Aug 16, 2014.

  1. infinitypbr

    infinitypbr

    Joined:
    Nov 28, 2012
    Posts:
    3,149
    Hello!

    I'm running my game on the iPad Air and iPhone 5s, and when there's about 10-12 enemies on the screen the frame rate drops a bit, not so much, but when there's 20+ enemies it drops to where it's kind of noticeable, about 10-15fps.

    A lot of the profiler time is in "Physics.Simulate", which lists three items all "N/A". I'm not sure why there's three listed.

    What goes into the Physics.Simulate category, and how might I optimize to increase the speed of those frames? Ther's also a bit of time on "NavMeshTransformAgents", which doesn't happen at all when running on the computer/editor.

    Thanks!!

     
  2. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,716
    Never move object with a collider that doesn't have a rigidbody.
    If you move an object with a rigidbody, use the rigidbody methods to move the object.
    Mesh collider should never have over 100 triangles, the lower, the better.
    You can try to subdivide them into smaller object, physic perform optimization by bounding volume first, before performing costly per-triangle collision.
    Most of the time, there is no need for your mesh collider to be 1:1 with your visual. Be sure you don't use your visual as mesh collider.
     
  3. Vanamerax

    Vanamerax

    Joined:
    Jan 12, 2012
    Posts:
    937
    You are most probably moving so called static colliders. These are colliders that have no rigidbody attached. Adviced is to either not move them, or add a rigidbody with isKinematic checked.
     
  4. infinitypbr

    infinitypbr

    Joined:
    Nov 28, 2012
    Posts:
    3,149
    Does a child object -- like a hit box on a limb -- with a collider count as "moving" if it's not specifically being moved? It's just following the arm which is being moved in the animation.
     
  5. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,716
    It does.
     
  6. infinitypbr

    infinitypbr

    Joined:
    Nov 28, 2012
    Posts:
    3,149
    Hrmm. I've added Kinematic Rigidbodies to those hit points (I think 5 per enemy), but the increase persists. It happens with enemies, and lowers as I kill them off, so it's definitely an increase due to them. Is this just normal?

     
  7. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,716
    So you passed from 16 ms to 11.5 ms? Sounds like improvement. Have you went over all the list of item I wrote?
     
  8. infinitypbr

    infinitypbr

    Joined:
    Nov 28, 2012
    Posts:
    3,149
    None of the enemies have mesh colliders -- just a capsule collider on the main enemy, and the 5 hit boxes on extremities. There's no reports of moving static colliders -- I saw those before with the "expensive operation" or what not listed in the profiler, and removed them. (they weren't on these enemies, however).

    I don't know which frame that was -- the cost of the physics goes up and down as you can see, and I didn't really notice a change in frame rate.
     
  9. infinitypbr

    infinitypbr

    Joined:
    Nov 28, 2012
    Posts:
    3,149
    Is there anything else that goes into Physics.Simulate besides colliders?
     
  10. infinitypbr

    infinitypbr

    Joined:
    Nov 28, 2012
    Posts:
    3,149
    I removed half of the colliders per enemy, but the cost is the same. (it's upwards of 30ms during the peaks). I'm going to try removing all the colluders and see what happens.

     
  11. infinitypbr

    infinitypbr

    Joined:
    Nov 28, 2012
    Posts:
    3,149
    That test did lower it -- I left on only the capsule collider on the main body. I'm testing once more with just that and the two hit boxes on the hands, since I want the player to be able to hit the enemy when they're stretching forward during an attack.
     
  12. infinitypbr

    infinitypbr

    Joined:
    Nov 28, 2012
    Posts:
    3,149
    Alright, it's down to 15ms now, which is a good improvement. I also removed a ray cast that was called when enemies are running away to make sure they stop before hitting a wall. I'll have to test that later, adding it back in, since i"m off to a wedding now.

    Any more ideas on what else i could do to improve the Physics.Simulate aspect? There's more optimization to be done in the scripts. Hopefully I can get this up to 30fps without lowering the number of enemies!

    Thanks for the help!

     
  13. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    Can you run with "Deep Profile" on? It'll show you more of the calls that are made instead of just the first call.

    I believe OnCollision and OnTrigger events also occur in Physics.Simulate. I was just looking at that the other day in our code and some Debug.Logs were eating up time in OnTriggerStay(). Deep Profile is what tipped me off.
     
  14. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,716
    What Garth said... But what I meant by Mesh collider may not be on the character/player. I assume your character collide against something, like a floor, wall, etc. Even if they don't move, if those mesh collider are poorly made, they will suck up major CPU cycles.

    Every mesh collider, that it moves or not, should be reviewed to know if they are properly made.
     
  15. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    show us your enemy movement code.
     
  16. infinitypbr

    infinitypbr

    Joined:
    Nov 28, 2012
    Posts:
    3,149
    @LightStriker The colliders are all primitive in this level, but I get what you mean now.

    @JamesLeeNZ I'll show you mine if you show me yours. Just kidding, I'm posting it below. I want to update it to use the navigation exclusively, but when I had it working like that before the enemies kind of ran in circles when they got close instead of going straight for an attack, so I switched it so when they're close they just walk forward to attack. However, I'm not really sure which is better or if one is faster than the other for CPU.

    It's long, sorry about that. Maybe it can be broken up into multiple scripts. [I've removed a bunch of superfluous stuff, but I'm not sure how to copy.paste the formatting so it's prettier]

    Code (csharp):
    1.  
    2. functionStart () {
    3. if (PlayerPrefs.GetInt("UseShadows") == 0 && blobShadow)
    4. {
    5. blobObject = Instantiate(blobShadow, transform.position, Quaternion.identity);
    6. blobObject.transform.parent = this.transform;
    7. blobObject.transform.eulerAngles = Vector3(90,0,0);
    8. blobObject.transform.localPosition.y = blobHeightOffset;
    9. }
    10. elseif (blobObject)
    11. Destroy(blobObject);
    12. playerInventory = GameObject.Find("PlayerInventory");
    13. questDirectory = GameObject.Find("PlayerQuestItems");
    14. playerObject = GameObject.Find("PlayerObject"); //SetplayerObject
    15. playerModel = GameObject.Find("PlayerModel"); //SetplayerObject
    16. if (!legacyAnimation)
    17. charAnimator = GetComponent(Animator);
    18. rigidbody.freezeRotation = true;
    19. hitPoints = hitPointsMax;
    20. screenWidth = Screen.width;
    21. screenHeight = Screen.height;
    22. charNav = GetComponent(NavMeshAgent);
    23. charNav.stoppingDistance = (newMeleeDistance * 0.8);
    24. charNav.updatePosition = false;
    25. charNav.acceleration = 270;
    26. charNav.enabled = false;
    27. weaponPointA.renderer.active = false;
    28. weaponPointB.renderer.active = false;
    29. hasSeenPlayer = false;
    30. InvokeRepeating("GetTitleController", 0, 0.2);
    31. }
    32.  
    33. functionDodgeForTime(time : float)
    34. {
    35. newDodged = true;
    36. yieldWaitForSeconds(time);
    37. newDodged = false;
    38. }
    39.  
    40. functionCastForTime(time : float)
    41. {
    42. newIsCasting = true;
    43. yieldWaitForSeconds(time);
    44. newIsCasting = false;
    45. }
    46.  
    47. functionStunForTime(time : float)
    48. {
    49. charAnimator.SetTrigger("dazed");
    50. charAnimator.SetBool("isDazed", true);
    51. newIsStunned = true;
    52. yieldWaitForSeconds(time);
    53. newIsStunned = false;
    54. }
    55.  
    56. functionKnockDownForTime(time : float)
    57. {
    58. newIsKnockedDown = true;
    59. yieldWaitForSeconds(time);
    60. newIsKnockedDown = false;
    61. if (!isDead)
    62. {
    63. rigidbody.constraints = RigidbodyConstraints.None;
    64. rigidbody.freezeRotation = true;
    65. }
    66. }
    67.  
    68. functionWaitSeconds(min : float, max : float){
    69. varrandomSeconds = Random.Range(min, max);
    70. yieldWaitForSeconds(randomSeconds);
    71. }
    72.  
    73. functionLookForPlayer(){
    74. Debug.DrawLine (playerModel.transform.position + Vector3(0,1,0), transform.position + Vector3(0,1,0), Color.red);
    75. if (!Physics.Linecast (playerModel.transform.position + Vector3(0,1,0), transform.position + Vector3(0,1,0), 1 << LayerMask.NameToLayer("MapSeen")) && !Physics.Linecast (playerModel.transform.position + Vector3(0,1,0), transform.position + Vector3(0,1,0), 1 << LayerMask.NameToLayer("MapHidden")) && !Physics.Linecast (playerModel.transform.position + Vector3(0,1,0), transform.position + Vector3(0,1,0), 1 << LayerMask.NameToLayer("BlockObject")))
    76. {
    77. hasSeenPlayer = true;
    78. newAttackTimer = newAttackTime;
    79. CancelInvoke("LookForPlayer");
    80. }
    81. }
    82.  
    83. functionLookAtThis(target : Vector3){
    84. if (!isDead && !newDodged)
    85. {
    86. desiredLookAt = target;
    87. varsplitLookAt2 : Vector3 = Vector3.Lerp(transform.position + transform.forward, desiredLookAt, 0.2);
    88. splitLookAt2.y = transform.position.y;
    89. Debug.DrawLine (transform.position + Vector3(0,1,0), transform.position + transform.forward + Vector3(0,1,0), Color.blue);
    90. Debug.DrawLine (transform.position + Vector3(0,1,0), desiredLookAt + Vector3(0,1,0), Color.red);
    91. Debug.DrawLine (transform.position + Vector3(0,1,0), splitLookAt2 + Vector3(0,1,0), Color.green);
    92. transform.LookAt(splitLookAt2);
    93. }
    94. }
    95.  
    96. functionStillSeePlayer(){
    97. if (!isDead)
    98. {
    99. if (Physics.Linecast (playerModel.transform.position + Vector3(0,1,0), transform.position + Vector3(0,1,0), 1 << LayerMask.NameToLayer("MapSeen")) && !Physics.Linecast (playerModel.transform.position + Vector3(0,1,0), transform.position + Vector3(0,1,0), 1 << LayerMask.NameToLayer("MapHidden")) && !Physics.Linecast (playerModel.transform.position + Vector3(0,1,0), transform.position + Vector3(0,1,0), 1 << LayerMask.NameToLayer("BlockObject")))
    100. {
    101. desiredLookAt = enemyStartPosition;
    102. hasSeenPlayer = false;
    103. charNav.speed = (moveSpeed * moveSpeedMod);
    104. charNav.enabled = true;
    105. charNav.destination = enemyStartPosition;
    106. isMoving = true;
    107. newDesDestination = enemyStartPosition;
    108. CancelInvoke("StillSeePlayer");
    109. }
    110. }
    111. }
    112.  
    113. functionGetTitleController(){
    114. if (levelControl == null)
    115. levelControl = GameObject.Find("LevelControl");
    116. if (!titleController)
    117. titleController = GameObject.Find("TitleController");
    118. if (!playerObject)
    119. playerObject = GameObject.Find("PlayerObject");
    120. if (!playerModel)
    121. playerModel = GameObject.Find("PlayerModel");
    122. if (!playerCamera)
    123. playerCamera = GameObject.Find("playerCamera").camera;
    124. if (!charNav)
    125. {
    126. charNav = GetComponent(NavMeshAgent);
    127. charNav.stoppingDistance = (newMeleeDistance * 0.8);
    128. if (!legacyAnimation)
    129. charNav.updatePosition = false;
    130. }
    131. if (levelControl && titleController && playerObject && playerModel && playerCamera && charNav)
    132. {
    133. scriptReady = true;
    134. CancelInvoke("GetTitleController");
    135. if (Vector3.Distance(transform.position, playerModel.transform.position) > 25)
    136. {
    137. if (enemyPlaceholder)
    138. {
    139. varnewPlacementObj = Instantiate(enemyPlaceholder, transform.position, Quaternion.identity);
    140. newPlacementObj.GetComponent(enemyPlaceholderScript).enemyModel = this.gameObject;
    141. this.gameObject.SetActive(false);
    142. }
    143. else
    144. {
    145. print ("enemyPlaceholderMissing! " + this.name);
    146. }
    147. }
    148. }
    149. }
    150.  
    151. functionSendSpeedToAnimator(){
    152. vardesiredDistance = Vector3.Distance(charNav.desiredVelocity, Vector3(0,0,0));
    153. currentVelocity = Mathf.Lerp(lastVelocity, desiredDistance, (Time.deltaTime * 2));
    154. lastVelocity = currentVelocity;
    155. if (!legacyAnimation)
    156. charAnimator.SetFloat("velocity", lastVelocity); //PasscurrentVelocitytoAnimator
    157. }
    158.  
    159. functionGetDistances(){
    160. playerNotNear += Time.deltaTime;
    161. if (playerNotNear >= 10)
    162. {
    163. SaveAndHide(0.0);
    164. }
    165. //distanceToPlayer = Vector3.Distance(transform.position, playerModel.transform.position); // Distance, EnemytoPlayer
    166. //distanceToStart = Vector3.Distance(transform.position, enemyStartPosition);
    167. }
    168.  
    169. functionSaveAndHide(delay : float){
    170. if (IsInvoking("LookForPlayer"))
    171. {
    172. CancelInvoke("LookForPlayer");
    173. }
    174. if (IsInvoking("StillSeePlayer"))
    175. {
    176. CancelInvoke("StillSeePlayer");
    177. }
    178. transform.position = enemyStartPosition;
    179. if (enemyPlaceholder)
    180. {
    181. varnewPlacementObj = Instantiate(enemyPlaceholder, transform.position, Quaternion.identity);
    182. newPlacementObj.GetComponent(enemyPlaceholderScript).enemyModel = this.gameObject;
    183. this.gameObject.SetActive(false);
    184. }
    185. else
    186. {
    187. print ("enemyPlaceholderMissing! " + this.name);
    188. }
    189. }
    190.  
    191. functionCheckForAnimationEnd(){
    192. varcheckCurrentBaseState = charAnimator.GetCurrentAnimatorStateInfo(0);
    193. if (checkCurrentBaseState.nameHash != Animator.StringToHash("BaseLayer.Attack1") &&
    194. checkCurrentBaseState.nameHash != Animator.StringToHash("BaseLayer.Attack2") &&
    195. checkCurrentBaseState.nameHash != Animator.StringToHash("BaseLayer.Attack3") &&
    196. checkCurrentBaseState.nameHash != Animator.StringToHash("BaseLayer.Attack4") &&
    197. checkCurrentBaseState.nameHash != Animator.StringToHash("BaseLayer.Attack5") &&
    198. checkCurrentBaseState.nameHash != Animator.StringToHash("BaseLayer.Attack5b") &&
    199. checkCurrentBaseState.nameHash != Animator.StringToHash("BaseLayer.Attack6") &&
    200. checkCurrentBaseState.nameHash != Animator.StringToHash("BaseLayer.Attack7") &&
    201. checkCurrentBaseState.nameHash != Animator.StringToHash("BaseLayer.Attack8") &&
    202. checkCurrentBaseState.nameHash != Animator.StringToHash("BaseLayer.Attack9") &&
    203. checkCurrentBaseState.nameHash != Animator.StringToHash("BaseLayer.Attack10") &&
    204. checkCurrentBaseState.nameHash != Animator.StringToHash("BaseLayer.Attack11") &&
    205. checkCurrentBaseState.nameHash != Animator.StringToHash("BaseLayer.Attack12") &&
    206. checkCurrentBaseState.nameHash != Animator.StringToHash("BaseLayer.Attack13") &&
    207. checkCurrentBaseState.nameHash != Animator.StringToHash("BaseLayer.Attack14") &&
    208. checkCurrentBaseState.nameHash != Animator.StringToHash("BaseLayer.Attack15") &&
    209. checkCurrentBaseState.nameHash != Animator.StringToHash("BaseLayer.Attack16") &&
    210. checkCurrentBaseState.nameHash != Animator.StringToHash("BaseLayer.Attack17") &&
    211. checkCurrentBaseState.nameHash != Animator.StringToHash("BaseLayer.Attack18") &&
    212. checkCurrentBaseState.nameHash != Animator.StringToHash("BaseLayer.Attack19") &&
    213. checkCurrentBaseState.nameHash != Animator.StringToHash("BaseLayer.Attack20") &&
    214. checkCurrentBaseState.nameHash != Animator.StringToHash("BaseLayer.Attack21") &&
    215. checkCurrentBaseState.nameHash != Animator.StringToHash("BaseLayer.Attack21b") &&
    216. checkCurrentBaseState.nameHash != Animator.StringToHash("BaseLayer.Attack22") &&
    217. checkCurrentBaseState.nameHash != Animator.StringToHash("BaseLayer.Attack22b") &&
    218. checkCurrentBaseState.nameHash != Animator.StringToHash("BaseLayer.Attack23") &&
    219. checkCurrentBaseState.nameHash != Animator.StringToHash("BaseLayer.Attack23b") &&
    220. checkCurrentBaseState.nameHash != Animator.StringToHash("BaseLayer.Attack24") &&
    221. checkCurrentBaseState.nameHash != Animator.StringToHash("BaseLayer.Attack24b") &&
    222. checkCurrentBaseState.nameHash != Animator.StringToHash("BaseLayer.Attack25") &&
    223. checkCurrentBaseState.nameHash != Animator.StringToHash("BaseLayer.Attack25b") &&
    224. checkCurrentBaseState.nameHash != Animator.StringToHash("BaseLayer.Attack26") &&
    225. checkCurrentBaseState.nameHash != Animator.StringToHash("BaseLayer.Attack26b") &&
    226. checkCurrentBaseState.nameHash != Animator.StringToHash("BaseLayer.Attack27") &&
    227. checkCurrentBaseState.nameHash != Animator.StringToHash("BaseLayer.Attack27b") &&
    228. checkCurrentBaseState.nameHash != Animator.StringToHash("BaseLayer.Attack28") &&
    229. checkCurrentBaseState.nameHash != Animator.StringToHash("BaseLayer.Attack28b") &&
    230. checkCurrentBaseState.nameHash != Animator.StringToHash("BaseLayer.Attack28c") &&
    231. checkCurrentBaseState.nameHash != Animator.StringToHash("BaseLayer.Attack28d") &&
    232. checkCurrentBaseState.nameHash != Animator.StringToHash("BaseLayer.Attack28e") &&
    233. checkCurrentBaseState.nameHash != Animator.StringToHash("BaseLayer.Attack28f") &&
    234. checkCurrentBaseState.nameHash != Animator.StringToHash("BaseLayer.Attack29") &&
    235. checkCurrentBaseState.nameHash != Animator.StringToHash("BaseLayer.Attack29b") &&
    236. checkCurrentBaseState.nameHash != Animator.StringToHash("BaseLayer.Attack29c") &&
    237. checkCurrentBaseState.nameHash != Animator.StringToHash("BaseLayer.Attack29d") &&
    238. checkCurrentBaseState.nameHash != Animator.StringToHash("BaseLayer.Attack29e") &&
    239. checkCurrentBaseState.nameHash != Animator.StringToHash("BaseLayer.Attack29f"))
    240. {
    241. checkAttackHit = false;
    242. EndCheckAttack();
    243. }
    244. }
    245.  
    246. functionCheckAttackHit(){
    247. varfixedP : Vector3 = transform.position;
    248. varfixedA : Vector3 = weaponPointA.transform.position;
    249. fixedP.y = transform.position.y + 1;
    250. fixedA.y = fixedP.y;
    251. vardistanceToPointA = Vector3.Distance(fixedP, fixedA);
    252. varweaponDistanceSphere = Physics.OverlapSphere(fixedP, distanceToPointA, 1 << LayerMask.NameToLayer("Player"));
    253. for (variOverlapAttack = 0; iOverlapAttack < weaponDistanceSphere.Length; iOverlapAttack++) {
    254. if (weaponDistanceSphere[iOverlapAttack].tag == "PlayerObject")
    255. {
    256. //Createavectorfromtheenemytotheplayerandstoretheanglebetweenitandforward.
    257. varhitDirection : Vector3 = weaponDistanceSphere[iOverlapAttack].transform.position - transform.position;
    258. varhitAngle : float = Vector3.Angle(hitDirection, transform.forward);
    259. //Iftheanglebetweenforwardandwheretheplayeris, islessthanhalftheangleofview...
    260. if(hitAngle < fieldOfView / 2)
    261. {
    262. if (!weaponDistanceSphere[iOverlapAttack].gameObject.transform.parent.gameObject.GetComponent(playerScript).isDead && !hitPlayer)
    263. {
    264. hitPlayer = true;
    265. ComputeDamage(weaponDistanceSphere[iOverlapAttack].gameObject.transform.parent.gameObject);
    266. }
    267. }
    268. }
    269. }
    270. }
    271.  
    272. functionEndCheckAttack(){
    273. lastPointA = Vector3(0,0,0);
    274. lastPointB = Vector3(0,0,0);
    275. }
    276.  
    277. functionUpdatePlayerDestination(){
    278. if (charNav.enabled && gameObject.activeSelf)
    279. {
    280.  
    281. charNav.enabled = false;
    282. charNav.enabled = true;
    283. charNav.updatePosition = false;
    284. charNav.stoppingDistance = (newMeleeDistance * 0.8);
    285. //charNav.updateRotation = true;
    286. charNav.updateRotation = false;
    287. lookAtPlayer = true;
    288. charNav.SetDestination(playerModel.transform.position);
    289. }
    290. }
    291.  
    292. functionDisableAnimator(){
    293. yieldWaitForSeconds(1.2);
    294. charAnimator.enabled = false;
    295. }
    296.  
    297. functionNewestAttackSytem(){
    298. if (useNewAttackSystem && !isDead)
    299. {
    300. if (charAnimator.enabled)
    301. {
    302. vardesiredDistance = Vector3.Distance(Vector3(0,0,0), charNav.desiredVelocity);
    303. currentVelocity = Mathf.Lerp(lastVelocity, desiredDistance, (Time.deltaTime * 2));
    304. lastVelocity = currentVelocity;
    305. }
    306. if (!inAttackMode)
    307. {
    308. if (newAttackTimer >= newAttackTime)
    309. {
    310. //lookAtPlayer = false;
    311. charNav.enabled = true;
    312. inAttackMode = true;
    313. runningToPlayer = true;
    314. charAnimator.SetBool("walkBackward", false);
    315. charAnimator.SetBool("defend", false);
    316. charAnimator.SetBool("crouch", false);
    317. charNav.SetDestination(playerModel.transform.position);
    318. //InvokeRepeating("UpdatePlayerDestination", 0, 0.5);
    319. }
    320. else
    321. {
    322. CancelInvoke("UpdatePlayerDestination");
    323. lookAtPlayer = true;
    324. charNav.enabled = false;
    325. newAttackTimer += Time.deltaTime * moveSpeedMod;
    326. }
    327. if ((distanceToPlayer <= newMeleeDistance * .75 || distanceToPlayer <= 1) && !inAttackMode)
    328. {
    329. varcheckBackAnimation = charAnimator.GetCurrentAnimatorStateInfo(0);
    330. varcheckBackAnimation2 = charAnimator.GetNextAnimatorStateInfo(0);
    331. if (checkBackAnimation.nameHash != charAnimator.StringToHash("BaseLayer.WalkBackward") && checkBackAnimation2.nameHash != charAnimator.StringToHash("BaseLayer.WalkBackward"))
    332. {
    333. charAnimator.SetBool("walkBackward", true);
    334. charAnimator.SetTrigger("startWalkBackward");
    335. }
    336. }
    337. else
    338. charAnimator.SetBool("walkBackward", false);
    339. }
    340. elseif (inAttackMode)
    341. {
    342. if (runningToPlayer)
    343. {
    344. if (!IsInvoking("UpdatePlayerDestination"))
    345. InvokeRepeating("UpdatePlayerDestination", 0, 0.2);
    346. charAnimator.SetFloat("velocity", lastVelocity);
    347. if (distanceToPlayer < newMeleeDistance)
    348. {
    349. attackTicker = 1.0;
    350. CancelInvoke("UpdatePlayerDestination");
    351. charNav.SetDestination(transform.position);
    352. lookAtPlayer = true;
    353. runningToPlayer = false;
    354. charNav.enabled = false;
    355. isAttacking = true;
    356. newAttackTimer = 0;
    357. varnewAttack : int = newMeleeAttacks[Random.Range(0, newMeleeAttacks.Length)];
    358. charAnimator.SetInteger("animationNumber", newAttack);
    359. charAnimator.SetTrigger("attack");
    360. }
    361. elseif (distanceToPlayer < newRangeDistance)
    362. {
    363. attackTicker = 1.5;
    364. CancelInvoke("UpdatePlayerDestination");
    365. lookAtPlayer = true;
    366. runningToPlayer = false;
    367. charNav.enabled = false;
    368. isAttacking = true;
    369. newAttackTimer = 0;
    370. varcastRandom = Random.Range(0, newRangeAttacks.Length);
    371. varnewCast : int = newRangeAttacks[castRandom];
    372. spellOnDeck = newSpells[castRandom];
    373. charAnimator.SetInteger("animationNumber", newCast);
    374. charAnimator.SetTrigger("cast");
    375. }
    376. }
    377. elseif (isAttacking)
    378. {
    379. //print ("isAttacking");
    380. if (charAnimator.GetFloat("velocity") > 0)
    381. SlowDown();
    382. if (attackTicker > 0)
    383. attackTicker -= Time.deltaTime;
    384. if (!charAnimator.IsInTransition(0))
    385. {
    386. if (attackTicker <= 0)
    387. {
    388. varcheckForIdle = charAnimator.GetCurrentAnimatorStateInfo(0);
    389. if (checkForIdle.nameHash == charAnimator.StringToHash("BaseLayer.Idle"))
    390. {
    391. lookAtPlayer = true;
    392. doingAttackAction = false;
    393. isAttacking = false;
    394. if (Random.Range(0,100) <= runAwayWhenDone)
    395. {
    396. movingToRunSpot = true;
    397. runSpotTimer = 0.0;
    398. lookAtPlayer = false;
    399. isMoving = true;
    400. runSpot = transform.position - (transform.forward * (newRunDistance - distanceToPlayer));
    401. charNav.enabled = true;
    402. charNav.SetDestination(runSpot);
    403. InvokeRepeating("CheckForBlockObject", 1, 0.5);
    404. }
    405. else
    406. {
    407. SlowDown();
    408. inAttackMode = false;
    409. charNav.enabled = false;
    410. }
    411. }
    412. }
    413. }
    414. }
    415. elseif (movingToRunSpot)
    416. {
    417. charAnimator.SetFloat("velocity", lastVelocity);
    418. if (charNav.remainingDistance <= charNav.stoppingDistance || runSpotTimer >= 3.0)
    419. {
    420. SlowDown();
    421. movingToRunSpot = false;
    422. isMoving = false;
    423. charNav.enabled = false;
    424. inAttackMode = false;
    425. CancelInvoke("CheckForBlockObject");
    426. if (defendWhenIdle)
    427. charAnimator.SetBool("defend", true);
    428. if (crouchWhenIdle)
    429. charAnimator.SetBool("crouch", true);
    430. }
    431. }
    432. }
    433. }
    434. }
    435.  
    436.  
    437. functionCheckForBlockObject(){
    438. varrunBlockCheck = transform.TransformDirection (Vector3.forward);
    439. varfoundBlock = false;
    440. varblockedBy : RaycastHit[];
    441. blockedBy = Physics.RaycastAll (transform.position + Vector3(0,0.3,0), runBlockCheck, 0.75);
    442. for (variBlocked = 0; iBlocked < blockedBy.Length; iBlocked++) {
    443. varblockedByObj = blockedBy[iBlocked].collider.gameObject;
    444. if (blockedByObj.tag != "ZoneControl" && blockedByObj.tag != "EnemyObject" && blockedByObj.tag != "PlayerObject" && blockedByObj.tag != "Player" && blockedByObj.tag != "Enemy")
    445. {
    446. foundBlock = true;
    447. }
    448. if (blockedByObj.tag == "EnemyBlockOnly")
    449. foundBlock = true;
    450. }
    451. if (foundBlock)
    452. {
    453. charAnimator.SetFloat("velocity", 0);
    454. charAnimator.applyRootMotion = true;
    455. movingToRunSpot = false;
    456. charNav.updatePosition = false;
    457. inAttackMode = false; //Waitfornewattacktime
    458. CancelInvoke("CheckForBlockObject");
    459. if (defendWhenIdle)
    460. charAnimator.SetBool("defend", true);
    461. if (crouchWhenIdle)
    462. charAnimator.SetBool("crouch", true);
    463. lookAtPlayer = true;
    464. }
    465. }
    466.  
    467. functionUpdate () {
    468. if (scriptReady)
    469. {
    470. if (movingToRunSpot)
    471. runSpotTimer += Time.deltaTime;
    472. CheckForPause();
    473. if (endAttackThisFrame)
    474. {
    475. checkAttackHit = false;
    476. endAttackThisFrame = false;
    477. }
    478. if (moveDownDead)
    479. transform.position.y -= Time.deltaTime * 0.4;
    480.  
    481. if (!isDead && !paused)
    482. {
    483. if (!hasSeenPlayer)
    484. {
    485. if (!IsInvoking("LookForPlayer"))
    486. InvokeRepeating("LookForPlayer", 0, 0.5);
    487. if (IsInvoking("CheckPlayerStatus"))
    488. CancelInvoke("CheckPlayerStatus");
    489. if (IsInvoking("StillSeePlayer"))
    490. CancelInvoke("StillSeePlayer");
    491. if (IsInvoking("CheckPlayerClose"))
    492. CancelInvoke("CheckPlayerClose");
    493. }
    494. elseif (hasSeenPlayer)
    495. {
    496. if (IsInvoking("LookForPlayer"))
    497. CancelInvoke("LookForPlayer");
    498. if (!IsInvoking("CheckPlayerStatus"))
    499. InvokeRepeating("CheckPlayerStatus", 0, 1.5);
    500. if (!IsInvoking("StillSeePlayer"))
    501. InvokeRepeating("StillSeePlayer", 0, 5.0);
    502. if (checkAttackHit)
    503. CheckForAnimationEnd();
    504. if (checkAttackHit)
    505. CheckAttackHit();
    506. if (!IsInvoking("CheckPlayerClose"))
    507. InvokeRepeating("CheckPlayerClose", 0, 0.1);
    508. if (!newIsKnockedDown && !newIsStunned && !newDodged)
    509. NewestAttackSytem();
    510.  
    511. }
    512. GetDistances();
    513. if (lookAtPlayer)
    514. LookAtThis(playerModel.transform.position);
    515. }
    516. }
    517. }
    518.  
    519. functionNewStartAttack(){
    520. SlowDown();
    521. attackDoubleCheck = 0;
    522. charNav.enabled = false;
    523. isMoving = false;
    524. isAttacking = true;
    525. newAttackTimer = 0;
    526. varnewAttack : int = newMeleeAttacks[Random.Range(0, newMeleeAttacks.Length)];
    527. charAnimator.SetInteger("animationNumber", newAttack);
    528. charAnimator.SetTrigger("attack");
    529. }
    530.  
    531. functionNewStartRange(){
    532. SlowDown();
    533. attackDoubleCheck = 0;
    534. charNav.enabled = false;
    535. isMoving = false;
    536. isAttacking = true;
    537. newAttackTimer = 0;
    538.  
    539. varcastRandom = Random.Range(0, newRangeAttacks.Length);
    540. varnewCast : int = newRangeAttacks[castRandom];
    541. spellOnDeck = newSpells[castRandom];
    542. charAnimator.SetInteger("animationNumber", newCast);
    543. charAnimator.SetTrigger("cast");
    544. }
    545.  
    546. functionNewCheckEndOfAttack(){
    547. varcheckBaseStateForAttackEnd = charAnimator.GetCurrentAnimatorStateInfo(0);
    548. if (attackDoubleCheck == 0)
    549. {
    550. if (!checkBaseStateForAttackEnd.IsName("BaseLayer.Idle"))
    551. attackDoubleCheck = 1;
    552. }
    553. elseif (attackDoubleCheck == 1)
    554. {
    555. if (checkBaseStateForAttackEnd.IsName("BaseLayer.Idle"))
    556. attackDoubleCheck = 2;
    557. }
    558. elseif (attackDoubleCheck == 2)
    559. NewEndOfAttack();
    560. }
    561.  
    562. functionNewEndOfAttack(){
    563. isAttacking = false;
    564. //CheckRunAwayHere
    565. if (Random.Range(0,100) < runAwayWhenDone)
    566. {
    567. varrunAwayPosition = transform.position - (transform.forward * (newRunDistance));
    568. runningAway = true;
    569. charNav.speed = (moveSpeed * moveSpeedMod);
    570. charNav.enabled = true;
    571. charNav.destination = runAwayPosition;
    572. isMoving = true;
    573. newDesDestination = runAwayPosition;
    574. }
    575. }
    576.  
    577. functionDoDeath(){
    578. yieldWaitForSeconds(0.0);
    579. if (deadParticle && !startDead)
    580. Instantiate(deadParticle, transform.position + Vector3(0,0,0), Quaternion.identity);
    581. yieldWaitForSeconds(1.75);
    582. moveDownDead = true;
    583. yieldWaitForSeconds(8.25);
    584. Destroy(gameObject);
    585. }
    586.  
    587.  
    588.  
    589. functionSlowDown(){
    590. if (!legacyAnimation)
    591. charAnimator.SetFloat("velocity", 0); //PasscurrentVelocitytoAnimator
    592. }
    593.  
    594.  
    595. functionComputeDamage(player : GameObject){
    596. vardamageGiven : float = 0.0;
    597. //Checkformiss (EnemySpeed + LuckRandom - PlayerAccuracy + LuckRandom = MissChancesof100)
    598. varmissed = 0;
    599. varplayerEvade = playerObject.GetComponent(playerScript).evadeBonus;
    600. varenemyLuckValue = Random.Range(0, luckBase * luckMod);
    601. varplayerLuckValue = Random.Range(0, (playerObject.GetComponent(playerScript).luckBase + playerObject.GetComponent(playerScript).luckEquipment) * playerObject.GetComponent(playerScript).luckMod);
    602. varenemyAccuracyValue = Random.Range(0, (accuracyBase * accuracyMod) + (enemyLuckValue * .25));
    603. varplayerSpeedValue = Random.Range(0, ((playerObject.GetComponent(playerScript).speedBase + playerObject.GetComponent(playerScript).speedEquipment) * playerObject.GetComponent(playerScript).speedMod) + (playerLuckValue * .25));
    604. varmissChances = 25 + (playerSpeedValue - enemyAccuracyValue);
    605. if (missChances > 0)
    606. {
    607. varmissRandom = Random.Range(1,100);
    608. if (missRandom <= missChances)
    609. missed = 1;
    610. if (missRandom > missChances && missRandom <= missChances + playerEvade && playerEvade != 0)
    611. missed = 2;
    612. }
    613. if (missed == 0)
    614. {
    615. //RandomDamagebasedonEnemyStats
    616. varweaponAttack = baseAttack + attackMod;
    617. varweaponDamage = damage + damageMod;
    618. for (varenemyAttack = 0; enemyAttack < weaponAttack; enemyAttack++) {
    619. damageGiven += Random.Range(1, weaponDamage);
    620. }
    621.  
    622.  
    623. //Addmight
    624. damageGiven += (mightBase + modMight) * mightMod;
    625. //AddRandomLuck
    626. damageGiven += (Random.Range(0, (luckBase + modLuck) * luckMod * .25));
    627. //AddBonusDamage
    628. damageGiven += Random.Range(bonusDamageMin, bonusDamageMax);
    629. //DeductforAttackType (Fire, Iceetc) BasedonEnemyResistance
    630. if (isPhysical)
    631. damageGiven = damageGiven - (damageGiven * ((playerObject.GetComponent(playerScript).resPhysical + playerObject.GetComponent(playerScript).resPhysicalEquipment * playerObject.GetComponent(playerScript).resPhysicalMod) / 100));
    632. if (isFire)
    633. damageGiven = damageGiven - (damageGiven * ((playerObject.GetComponent(playerScript).resFire + playerObject.GetComponent(playerScript).resFireEquipment * playerObject.GetComponent(playerScript).resFireMod) / 100));
    634. if (isIce)
    635. damageGiven = damageGiven - (damageGiven * ((playerObject.GetComponent(playerScript).resIce + playerObject.GetComponent(playerScript).resIceEquipment * playerObject.GetComponent(playerScript).resIceMod) / 100));
    636. if (isMagic)
    637. damageGiven = damageGiven - (damageGiven * ((playerObject.GetComponent(playerScript).resMagic + playerObject.GetComponent(playerScript).resMagicEquipment * playerObject.GetComponent(playerScript).resMagicMod) / 100));
    638. //DeductEnemyArmor
    639. damageGiven -= (playerObject.GetComponent(playerScript).armorBase + playerObject.GetComponent(playerScript).armorEquipment) * playerObject.GetComponent(playerScript).armorMod;
    640. //DeductPlayerLuck
    641. damageGiven -= playerLuckValue * .25;
    642.  
    643. //DoubleDamageOpportunity
    644.  
    645. //StunOpportunity
    646.  
    647.  
    648. if (damageGiven < 1)
    649. damageGiven = 1;
    650. damageGiven = Mathf.Floor(damageGiven);
    651. if (attackSound.Length != 0)
    652. {
    653. varattackPlay : int = Random.Range(0, attackSound.Length);
    654. audio.PlayClipAtPoint(attackSound[attackPlay], transform.position);
    655. }
    656. }
    657. else
    658. {
    659. if (missSound.Length != 0)
    660. {
    661. varmissPlay : int = Random.Range(0, missSound.Length);
    662. audio.PlayClipAtPoint(missSound[missPlay], transform.position);
    663. }
    664. }
    665. damageGiven = Mathf.Clamp(damageGiven, 1, 9999);
    666. playerObject.GetComponent(playerScript).ReceiveDamage(damageGiven, missed, this.gameObject, false, 0.0, false);
    667. }
    668.  
    669.  
    670.  
    671.  
    672. functionAttackForward(radius : float, physical : boolean, fire : boolean, ice : boolean, magic : boolean, bonusMin : float, bonusMax : float, delay : float, attack : int, damage : float, pushbackAmount : float, particle : GameObject, oneShot : boolean, destroyOnHit : boolean, modMight : float, modStamina : float, modWisdom : float, modAccuracy : float, modSpeed : float, modLuck : float, modMovement : float, modFire : float, modIce : float, modMagic : float, modPhysical : float, modTimeLimit : float, modArmor : float, yOffset : float)
    673. {
    674. yieldWaitForSeconds(delay); //Waitfordelay
    675. if (particle)
    676. {
    677. varattackFwd = transform.position + transform.TransformDirection (Vector3.forward); //Oneunitinfrontofcharacter
    678. varattackTargetPos = transform.position + transform.TransformDirection (Vector3.forward * 100); //100unitsinfrontofcharacter
    679. attackFwd.y += yOffset;
    680. attackTargetPos.y += yOffset;
    681. Instantiate(particle, attackFwd, Quaternion.identity); //Createthepartice
    682. }
    683. }
    684.  
    685. functionShootForward(physical : boolean, fire : boolean, ice : boolean, magic : boolean, bonusMin : float, bonusMax : float, delay : float, attack : int, damage : float, speed : float, particle : GameObject, follows : boolean, oneShot : boolean, destroyOnHit : boolean, modMight : float, modStamina : float, modWisdom : float, modAccuracy : float, modSpeed : float, modLuck : float, modMovement : float, modFire : float, modIce : float, modMagic : float, modPhysical : float, modTimeLimit : float, modArmor : float, yOffset : float)
    686. {
    687.  
    688. yieldWaitForSeconds(delay); //Waitfordelay
    689. varcastFwd = transform.position + transform.TransformDirection (Vector3.forward); //Oneunitinfrontofcharacter
    690. vartargetPos = transform.position + transform.TransformDirection (Vector3.forward * 100); //100unitsinfrontofcharacter
    691. castFwd.y += yOffset;
    692. targetPos.y += yOffset;
    693. varcastForwardParticle = Instantiate(particle, castFwd, Quaternion.identity); //Createtheparticle
    694. castForwardParticle.GetComponent(particleScript).wisdom = (wisdomBase * wisdomMod);
    695. castForwardParticle.GetComponent(particleScript).physical = physical;
    696. castForwardParticle.GetComponent(particleScript).fire = fire;
    697. castForwardParticle.GetComponent(particleScript).ice = ice;
    698. castForwardParticle.GetComponent(particleScript).magic = magic;
    699. castForwardParticle.GetComponent(particleScript).bonusMin = bonusMin;
    700. castForwardParticle.GetComponent(particleScript).bonusMax = bonusMax;
    701. castForwardParticle.GetComponent(particleScript).attack = attack;
    702. castForwardParticle.GetComponent(particleScript).damage = damage;
    703. castForwardParticle.GetComponent(particleScript).speed = speed;
    704. castForwardParticle.GetComponent(particleScript).targetPos = targetPos;
    705. castForwardParticle.GetComponent(particleScript).followsTarget = follows;
    706. castForwardParticle.GetComponent(particleScript).destroyOnHit = destroyOnHit;
    707. castForwardParticle.GetComponent(particleScript).oneShot = oneShot;
    708. castForwardParticle.GetComponent(particleScript).targetPlayer = true;
    709. castForwardParticle.GetComponent(particleScript).modMight = modMight;
    710. castForwardParticle.GetComponent(particleScript).modStamina = modStamina;
    711. castForwardParticle.GetComponent(particleScript).modWisdom = modWisdom;
    712. castForwardParticle.GetComponent(particleScript).modAccuracy = modAccuracy;
    713. castForwardParticle.GetComponent(particleScript).modSpeed = modSpeed;
    714. castForwardParticle.GetComponent(particleScript).modLuck = modLuck;
    715. castForwardParticle.GetComponent(particleScript).modMovement = modMovement;
    716. castForwardParticle.GetComponent(particleScript).modFire = modFire;
    717. castForwardParticle.GetComponent(particleScript).modIce = modIce;
    718. castForwardParticle.GetComponent(particleScript).modPhysical = modPhysical;
    719. castForwardParticle.GetComponent(particleScript).modMagic = modMagic;
    720. castForwardParticle.GetComponent(particleScript).modTimeLimit = modTimeLimit;
    721. castForwardParticle.GetComponent(particleScript).modArmor = modArmor;
    722. }
    723.  
    724. functionAttackMapPosition(radius : float, physical : boolean, fire : boolean, ice : boolean, magic : boolean, bonusMin : float, bonusMax : float, delay : float, attack : int, damage : float, particle : GameObject, speed : float, follows : boolean, oneShot : boolean, destroyOnHit : boolean, modMight : float, modStamina : float, modWisdom : float, modAccuracy : float, modSpeed : float, modLuck : float, modMovement : float, modFire : float, modIce : float, modMagic : float, modPhysical : float, modTimeLimit : float, modArmor : float, yOffset : float)
    725. {
    726. //NOTUSEDFORENEMIES.
    727. }
    728.  
    729. functionCastMapPosition(radius : float, physical : boolean, fire : boolean, ice : boolean, magic : boolean, bonusMin : float, bonusMax : float, delay : float, attack : int, damage : float, particle : GameObject, speed : float, follows : boolean, oneShot : boolean, destroyOnHit : boolean, modMight : float, modStamina : float, modWisdom : float, modAccuracy : float, modSpeed : float, modLuck : float, modMovement : float, modFire : float, modIce : float, modMagic : float, modPhysical : float, modTimeLimit : float, modArmor : float, yOffset : float)
    730. {
    731. //NOTUSEDFORENEMIES.
    732. }
    733.  
    734. functionCastPlayerPosition(radius : float, physical : boolean, fire : boolean, ice : boolean, magic : boolean, bonusMin : float, bonusMax : float, delay : float, attack : int, damage : float, particle : GameObject, speed : float, follows : boolean, oneShot : boolean, destroyOnHit : boolean, modMight : float, modStamina : float, modWisdom : float, modAccuracy : float, modSpeed : float, modLuck : float, modMovement : float, modFire : float, modIce : float, modMagic : float, modPhysical : float, modTimeLimit : float, modArmor : float, yOffset : float)
    735. {
    736. //Thishappenswithacastattackonplayerposition (likemagicthattravels, alwaysgoingtowardstheplayer) -- aguidedmissile
    737. yieldWaitForSeconds(delay);
    738. varcastPlayerFwd = transform.position + transform.TransformDirection (Vector3.forward); //Oneunitinfrontofcharacter
    739. vartargetPlayerPos = playerModel.transform.position; //PositionofPlayer
    740. castPlayerFwd.y += yOffset;
    741. targetPlayerPos.y += yOffset;
    742. varcastPlayerParticle = Instantiate(particle, castPlayerFwd, Quaternion.identity); //Createtheparticle
    743. castPlayerParticle.GetComponent(particleScript).wisdom = (wisdomBase * wisdomMod);
    744. //Passvariablesontoparticle (isatrigger, won'thaveeffectuntilithits)
    745. castPlayerParticle.GetComponent(particleScript).physical = physical;
    746. castPlayerParticle.GetComponent(particleScript).fire = fire;
    747. castPlayerParticle.GetComponent(particleScript).ice = ice;
    748. castPlayerParticle.GetComponent(particleScript).magic = magic;
    749. castPlayerParticle.GetComponent(particleScript).bonusMin = bonusMin;
    750. castPlayerParticle.GetComponent(particleScript).bonusMax = bonusMax;
    751. castPlayerParticle.GetComponent(particleScript).attack = attack;
    752. castPlayerParticle.GetComponent(particleScript).damage = damage;
    753. castPlayerParticle.GetComponent(particleScript).speed = speed;
    754. castPlayerParticle.GetComponent(particleScript).targetPos = targetPlayerPos;
    755. castPlayerParticle.GetComponent(particleScript).targetObj = playerModel;
    756. castPlayerParticle.GetComponent(particleScript).followsTarget = follows;
    757. castPlayerParticle.GetComponent(particleScript).destroyOnHit = destroyOnHit;
    758. castPlayerParticle.GetComponent(particleScript).oneShot = oneShot;
    759. castPlayerParticle.GetComponent(particleScript).targetPlayer = true;
    760. castPlayerParticle.GetComponent(particleScript).modMight = modMight;
    761. castPlayerParticle.GetComponent(particleScript).modStamina = modStamina;
    762. castPlayerParticle.GetComponent(particleScript).modWisdom = modWisdom;
    763. castPlayerParticle.GetComponent(particleScript).modAccuracy = modAccuracy;
    764. castPlayerParticle.GetComponent(particleScript).modSpeed = modSpeed;
    765. castPlayerParticle.GetComponent(particleScript).modLuck = modLuck;
    766. castPlayerParticle.GetComponent(particleScript).modMovement = modMovement;
    767. castPlayerParticle.GetComponent(particleScript).modFire = modFire;
    768. castPlayerParticle.GetComponent(particleScript).modIce = modIce;
    769. castPlayerParticle.GetComponent(particleScript).modPhysical = modPhysical;
    770. castPlayerParticle.GetComponent(particleScript).modMagic = modMagic;
    771. castPlayerParticle.GetComponent(particleScript).modTimeLimit = modTimeLimit;
    772. castPlayerParticle.GetComponent(particleScript).modArmor = modArmor;
    773. castPlayerParticle.GetComponent(particleScript).yOffset = yOffset;
    774. }
    775.  
    776. functionHelpEnemy(radius : float, physical : boolean, fire : boolean, ice : boolean, magic : boolean, bonusMin : float, bonusMax : float, delay : float, attack : int, damage : float, particle : GameObject, oneShot : boolean, destroyOnHit : boolean, modMight : float, modStamina : float, modWisdom : float, modAccuracy : float, modSpeed : float, modLuck : float, modMovement : float, modFire : float, modIce : float, modMagic : float, modPhysical : float, modTimeLimit : float, modArmor : float, yOffset : float)
    777. {
    778. yieldWaitForSeconds(delay);
    779. //Searchfornearestenemywhoisclosest. Ifnone, willdefertoHelpSelf
    780. varenemyToHelp : GameObject = GetEnemy(radius);
    781. if (!enemyToHelp)
    782. enemyToHelp = gameObject;
    783. varhelpEnemyParticlePosition = enemyToHelp.transform.position; //PositionofSelf
    784. helpEnemyParticlePosition.y += yOffset;
    785. varhelpEnemyParticle = Instantiate(particle, helpEnemyParticlePosition, Quaternion.identity); //Create
    786. helpEnemyParticle.GetComponent(particleScript).targetObj = enemyToHelp;
    787. helpEnemyParticle.GetComponent(particleScript).stayOnTarget = true;
    788. helpEnemyParticle.GetComponent(particleScript).yOffset = yOffset;
    789. //2. Computedamage: attackddamage + might - armor
    790. varhelpEnemy : float = 0.00;
    791. for(varn = 0; n < attack; n++)
    792. {
    793. helpEnemy += Random.Range(1, damage);
    794. }
    795. //AddBonushelpEnemy
    796. vargiveBonusHelpEnemy = Random.Range(bonusDamageMin, bonusDamageMax);
    797. helpEnemy = helpEnemy + giveBonusHelpEnemy;
    798. //EnsurefinalhelpEnemyisnotbelow0
    799. helpEnemy = Mathf.Clamp(helpEnemy, 0, 9999);
    800. //4. SendhelpEnemy, ifdidn'tdodge, toplayer
    801. enemyToHelp.GetComponent(enemyScript).ReceiveDamage(-helpEnemy, true);
    802. //5. Modifications
    803. if (modMight != 0.0 && enemyToHelp.GetComponent(enemyScript).mightModTime < modTimeLimit)
    804. {
    805. enemyToHelp.GetComponent(enemyScript).mightMod = modMight;
    806. enemyToHelp.GetComponent(enemyScript).mightModTime = modTimeLimit;
    807. }
    808. if (modStamina != 0.0 && enemyToHelp.GetComponent(enemyScript).staminaModTime < modTimeLimit)
    809. {
    810. enemyToHelp.GetComponent(enemyScript).staminaMod = modStamina;
    811. enemyToHelp.GetComponent(enemyScript).staminaModTime = modTimeLimit;
    812. }
    813. if (modWisdom != 0.0 && enemyToHelp.GetComponent(enemyScript).wisdomModTime < modTimeLimit)
    814. {
    815. enemyToHelp.GetComponent(enemyScript).wisdomMod = modWisdom;
    816. enemyToHelp.GetComponent(enemyScript).wisdomModTime = modTimeLimit;
    817. }
    818. if (modSpeed != 0.0 && enemyToHelp.GetComponent(enemyScript).speedModTime < modTimeLimit)
    819. {
    820. enemyToHelp.GetComponent(enemyScript).speedMod = modSpeed;
    821. enemyToHelp.GetComponent(enemyScript).speedModTime = modTimeLimit;
    822. }
    823. if (modAccuracy != 0.0 && enemyToHelp.GetComponent(enemyScript).accuracyModTime < modTimeLimit)
    824. {
    825. enemyToHelp.GetComponent(enemyScript).accuracyMod = modAccuracy;
    826. enemyToHelp.GetComponent(enemyScript).accuracyModTime = modTimeLimit;
    827. }
    828. if (modLuck != 0.0 && enemyToHelp.GetComponent(enemyScript).luckModTime < modTimeLimit)
    829. {
    830. enemyToHelp.GetComponent(enemyScript).luckMod = modLuck;
    831. enemyToHelp.GetComponent(enemyScript).luckModTime = modTimeLimit;
    832. }
    833. if (modArmor != 0.0 && enemyToHelp.GetComponent(enemyScript).armorModTime < modTimeLimit)
    834. {
    835. enemyToHelp.GetComponent(enemyScript).armorMod = modArmor;
    836. enemyToHelp.GetComponent(enemyScript).armorModTime = modTimeLimit;
    837. }
    838. if (modFire != 0.0 && enemyToHelp.GetComponent(enemyScript).fireModTime < modTimeLimit)
    839. {
    840. enemyToHelp.GetComponent(enemyScript).resFireMod = modFire;
    841. enemyToHelp.GetComponent(enemyScript).fireModTime = modTimeLimit;
    842. }
    843. if (modIce != 0.0 && enemyToHelp.GetComponent(enemyScript).iceModTime < modTimeLimit)
    844. {
    845. enemyToHelp.GetComponent(enemyScript).resIceMod = modIce;
    846. enemyToHelp.GetComponent(enemyScript).iceModTime = modTimeLimit;
    847. }
    848. if (modPhysical != 0.0 && enemyToHelp.GetComponent(enemyScript).physicalModTime < modTimeLimit)
    849. {
    850. enemyToHelp.GetComponent(enemyScript).resPhysicalMod = modPhysical;
    851. enemyToHelp.GetComponent(enemyScript).physicalModTime = modTimeLimit;
    852. }
    853. if (modMagic != 0.0 && enemyToHelp.GetComponent(enemyScript).magicModTime < modTimeLimit)
    854. {
    855. enemyToHelp.GetComponent(enemyScript).resMagicMod = modMagic;
    856. enemyToHelp.GetComponent(enemyScript).magicModTime = modTimeLimit;
    857. }
    858. if (modMovement != 0.0 && enemyToHelp.GetComponent(enemyScript).moveSpeedModTime < modTimeLimit)
    859. {
    860. enemyToHelp.GetComponent(enemyScript).moveSpeedMod = modMovement;
    861. enemyToHelp.GetComponent(enemyScript).moveSpeedModTime = modTimeLimit;
    862. }
    863.  
    864.  
    865. }
    866.  
    867. functionGetEnemy(radius : float) : GameObject
    868. {
    869. varcolliders : Collider[] = Physics.OverlapSphere (transform.position, radius);
    870. varclosestCollider : GameObject;
    871. for (varhit : Colliderincolliders) {
    872. //checksifit'S***tingitself
    873. if(hit.collider != transform.collider && hit.collider.gameObject.tag == "EnemyObject")
    874. {
    875. if(!closestCollider)
    876. {
    877. closestCollider = hit.collider.gameObject;
    878. }
    879. //comparesdistances
    880. if(Vector3.Distance(transform.position, hit.transform.position) <= Vector3.Distance(transform.position, closestCollider.transform.position))
    881. {
    882. closestCollider = hit.collider.gameObject;
    883. }
    884. }
    885. }
    886. returnclosestCollider;
    887. }
    888.  
    889.  
    890. functionHelpSelf(radius : float, physical : boolean, fire : boolean, ice : boolean, magic : boolean, bonusMin : float, bonusMax : float, delay : float, attack : int, damage : float, particle : GameObject, oneShot : boolean, destroyOnHit : boolean, modMight : float, modStamina : float, modWisdom : float, modAccuracy : float, modSpeed : float, modLuck : float, modMovement : float, modFire : float, modIce : float, modMagic : float, modPhysical : float, modTimeLimit : float, modArmor : float, yOffset : float)
    891. {
    892. yieldWaitForSeconds(delay);
    893. varhelpSelfParticlePosition = transform.position; //PositionofSelf
    894. helpSelfParticlePosition.y += yOffset;
    895. varhelpSelfParticle = Instantiate(particle, helpSelfParticlePosition, Quaternion.identity); //Create
    896. helpSelfParticle.GetComponent(particleScript).targetObj = gameObject;
    897. helpSelfParticle.GetComponent(particleScript).stayOnTarget = true;
    898. helpSelfParticle.GetComponent(particleScript).yOffset = yOffset;
    899. //2. Computedamage: attackddamage + might - armor
    900. varhelpSelf : float = 0.00;
    901. for(varn = 0; n < attack; n++)
    902. {
    903. helpSelf += Random.Range(1, damage);
    904. }
    905. //AddBonushelpSelf
    906. vargiveBonusHelpSelf = Random.Range(bonusDamageMin, bonusDamageMax);
    907. helpSelf = helpSelf + giveBonusHelpSelf;
    908. //EnsurefinalhelpSelfisnotbelow0
    909. helpSelf = Mathf.Clamp(helpSelf, 0, 9999);
    910. //4. SendhelpSelf, ifdidn'tdodge, toplayer
    911. ReceiveDamage(-helpSelf, true, false, 0.0, false, false);
    912. //5. Modifications
    913. if (modMight != 0.0 && mightModTime < modTimeLimit)
    914. {
    915. mightMod = modMight;
    916. mightModTime = modTimeLimit;
    917. }
    918. if (modStamina != 0.0 && staminaModTime < modTimeLimit)
    919. {
    920. staminaMod = modStamina;
    921. staminaModTime = modTimeLimit;
    922. }
    923. if (modWisdom != 0.0 && wisdomModTime < modTimeLimit)
    924. {
    925. wisdomMod = modWisdom;
    926. wisdomModTime = modTimeLimit;
    927. }
    928. if (modSpeed != 0.0 && speedModTime < modTimeLimit)
    929. {
    930. speedMod = modSpeed;
    931. speedModTime = modTimeLimit;
    932. }
    933. if (modAccuracy != 0.0 && accuracyModTime < modTimeLimit)
    934. {
    935. accuracyMod = modAccuracy;
    936. accuracyModTime = modTimeLimit;
    937. }
    938. if (modLuck != 0.0 && luckModTime < modTimeLimit)
    939. {
    940. luckMod = modLuck;
    941. luckModTime = modTimeLimit;
    942. }
    943. if (modArmor != 0.0 && armorModTime < modTimeLimit)
    944. {
    945. armorMod = modArmor;
    946. armorModTime = modTimeLimit;
    947. }
    948. if (modFire != 0.0 && fireModTime < modTimeLimit)
    949. {
    950. resFireMod = modFire;
    951. fireModTime = modTimeLimit;
    952. }
    953. if (modIce != 0.0 && iceModTime < modTimeLimit)
    954. {
    955. resIceMod = modIce;
    956. iceModTime = modTimeLimit;
    957. }
    958. if (modPhysical != 0.0 && physicalModTime < modTimeLimit)
    959. {
    960. resPhysicalMod = modPhysical;
    961. physicalModTime = modTimeLimit;
    962. }
    963. if (modMagic != 0.0 && magicModTime < modTimeLimit)
    964. {
    965. resMagicMod = modMagic;
    966. magicModTime = modTimeLimit;
    967. }
    968. if (modMovement != 0.0 && moveSpeedModTime < modTimeLimit)
    969. {
    970. moveSpeedMod = modMovement;
    971. moveSpeedModTime = modTimeLimit;
    972. }
    973. }
    974.  
    975. functionSetPostDodgeDestination(){
    976. inAttackMode = false;
    977. SetDestHere();
    978. }
    979.  
    980. //Runwhencharacterreceivesdamage. Showsthedamageamountonscreen.
    981. functionReceiveDamage(damage : float, missed : boolean, damageOverTime : boolean, vampiric : float, doubleDamage : boolean, stunDamage : boolean)
    982. {
    983. inAttackMode = false;
    984. if (!isDead && !playerObject.GetComponent(playerScript).isDead)
    985. {
    986. //if (missed)
    987. //damage = 0;
    988. //USETHISFORBeingSTunned!
    989. if (!damageOverTime)
    990. {
    991. if (!missed)
    992. {
    993. SlowDown();
    994. charNav.enabled = false;
    995. movingToRangeAttack = false;
    996. movingToMeleeAttack = false;
    997. movingToRunSpot = false;
    998. inAttackMode = false;
    999. isAttacking = false;
    1000. checkAttackHit = false;
    1001. newAttackTimer = 0;
    1002. }
    1003. if (gotStunned || stunDamage && !noStun && !isDead) //damage >= hitPointsMax * 0.25
    1004. {
    1005. if (!legacyAnimation)
    1006. StunForTime(4.0);
    1007. motionPattern = 0;
    1008. patternStep = 0;
    1009. attackStep = 0;
    1010. KnockDownForTime(5.0);
    1011. rigidbody.constraints = RigidbodyConstraints.FreezePositionX | RigidbodyConstraints.FreezePositionY | RigidbodyConstraints.FreezePositionZ;
    1012. rigidbody.freezeRotation = true;
    1013. gotStunned = false;
    1014. }
    1015. else
    1016. {
    1017. if (!legacyAnimation)
    1018. charAnimator.SetBool("highDamage", false);
    1019. }
    1020. varenemyDot = transform.forward;
    1021. varplayerDot = playerModel.transform.forward;
    1022. vardotResult = Vector3.Dot(enemyDot, playerDot);
    1023. if (dotResult >= 0)
    1024. {
    1025. if (!legacyAnimation)
    1026. charAnimator.SetBool("sameDirection", true);
    1027. }
    1028. else
    1029. {
    1030. if (!legacyAnimation)
    1031. charAnimator.SetBool("sameDirection", false);
    1032. }
    1033. if (damage > 0 && !missed && !stunDamage)
    1034. {
    1035. AttackEnd();
    1036. if (!legacyAnimation && !newIsStunned)
    1037. charAnimator.SetTrigger("gotHit");
    1038. }
    1039. elseif (missed && dotResult < 0 && !stunDamage && !newIsStunned)
    1040. {
    1041. if (!legacyAnimation)
    1042. {
    1043. inAttackMode = false;
    1044. newAttackTimer = 0;
    1045. charAnimator.SetInteger("animationNumber", 1);
    1046. charAnimator.SetTrigger("dodgeAttack");
    1047. charAnimator.SetFloat("velocity", 0);
    1048. }
    1049. DodgeForTime(1.0);
    1050. }
    1051.  
    1052. if (damage != 0)
    1053. {
    1054. GetComponent(enemyHeathBar).gotHitTimer = 3.0;
    1055. GetComponent(enemyHeathBar).barAlpha = 1.0;
    1056. }
    1057. }
    1058. else
    1059. {
    1060. GetComponent(enemyHeathBar).gotHitTimer = 3.0;
    1061. GetComponent(enemyHeathBar).barAlpha = 1.0;
    1062. }
    1063.  
    1064. if (vampiric != 0.0)
    1065. {
    1066. playerObject.GetComponent(playerScript).ReceiveDamage(-damage * vampiric, 0, playerObject, false, 0.0, true);
    1067. //playerObject.GetComponent(playerScript).health = Mathf.Clamp(playerObject.GetComponent(playerScript).health + (damage * vampiric), 0, playerObject.GetComponent(playerScript).maxHealth);
    1068. //playerObject.GetComponent(playerScript).health = Mathf.Clamp(playerObject.GetComponent(playerScript).health + (damage * vampiric), 0, playerObject.GetComponent(playerScript).maxHealth);
    1069. }
    1070. hitPoints = (Mathf.Clamp(hitPoints - damage, 0, hitPointsMax)); //SubtractdamagefromhitPoints, makesureitdoesn'tfallbelow0
    1071. if (!playerCamera)
    1072. playerCamera = GameObject.Find("playerCamera").camera;
    1073. varscreenPos = playerCamera.WorldToScreenPoint(transform.position); //FindthescreenPositionoftheplayer
    1074. vartextPos = Vector3(screenPos.x / screenWidth, (screenPos.y / screenHeight) + 0.15, 0); //Placethetextposition
    1075. vartextResize = screenWidth / 482; //Resizethetextbasedonusersscreensize
    1076. if (!damageOverTime)
    1077. {
    1078. vardamageReport = Instantiate(damageReport, textPos, Quaternion.identity); //InstantiatethedamageReportprefab
    1079. if (damage >= 0 && !missed) //If 'damage' is >= 0itmeansthey'vebeenattacked
    1080. damageReport.GetComponent(GUIText).text = "-" + Mathf.Round(damage); //Changethetexttosaythecorrectnumber
    1081. elseif (damage <= 0 && !missed) //if 'damage' islessthan0, they'rebeinghealed
    1082. {
    1083. damage = damage * -1; //removedthenegativesign
    1084. damageReport.GetComponent(GUIText).text = "+" + Mathf.Round(damage); //Display + damageforhealing
    1085. }
    1086. else
    1087. damageReport.GetComponent(GUIText).text = "Missed!";
    1088. if (doubleDamage)
    1089. damageReport.GetComponent(GUIText).color = Color(.8,0,0);
    1090. if (stunDamage)
    1091. damageReport.GetComponent(GUIText).color = Color(.3,.3,.8);
    1092. }
    1093.  
    1094. if (hitPoints <= 0)
    1095. {
    1096.  
    1097. checkAttackHit = false;
    1098. //ChooseDeathAnimation
    1099. vardeathAnimation = Random.Range(1,4);
    1100. if (!legacyAnimation)
    1101. {
    1102. if (deathAnimation == 1)
    1103. charAnimator.SetInteger("animationNumber", 1);
    1104. if (deathAnimation == 2)
    1105. charAnimator.SetInteger("animationNumber", 2);
    1106. if (deathAnimation == 3)
    1107. charAnimator.SetInteger("animationNumber", 3);
    1108. if (deathAnimation == 4)
    1109. charAnimator.SetInteger("animationNumber", 4);
    1110.  
    1111. }
    1112. isDead = true;
    1113. charNav.enabled = false;
    1114. charAnimator.SetBool("isDead", true);
    1115. charAnimator.SetBool("walkBackward", false);
    1116. charAnimator.SetBool("startWalkBackward", false);
    1117. charAnimator.SetTrigger("die");
    1118. DidDie(); //Ifplayerhealthis0, runtheDidDiefunction
    1119. }
    1120. else
    1121. {
    1122. //PlaygotHitSound
    1123. if (gotHitSound.Length != 0 && damage != 0 && !missed && !damageOverTime)
    1124. {
    1125. vargotHitPlay : int = Random.Range(0, gotHitSound.Length);
    1126. audio.PlayClipAtPoint(gotHitSound[gotHitPlay], transform.position);
    1127. }
    1128. }
    1129. if ((Random.Range(0,100) < retaliateChance || attackAfterHit) && !damageOverTime)
    1130. newAttackTimer = newAttackTime;
    1131. }
    1132. }
    1133.  
    1134. functionAttackStart(angle : int){
    1135. if (!isDead)
    1136. {
    1137. fieldOfView = angle;
    1138. checkAttackHit = true;
    1139. hitPlayer = false;
    1140. }
    1141. }
    1142.  
    1143. functionAttackEnd(){
    1144. endAttackThisFrame = true;
    1145. }
    1146.  
    1147. functionCheckForHit(posA : Vector3, posB : Vector3){
    1148. //LinecastfromposAtoposB
    1149.  
    1150. varposDistance = Vector3.Distance(posA, posB);
    1151. varhitInfo : RaycastHit[];
    1152. varattackHits : RaycastHit[];
    1153. attackHits = Physics.RaycastAll (posA, (posB - posA).normalized, posDistance, 1 << LayerMask.NameToLayer("Player"));
    1154. for (variAttack = 0; iAttack < attackHits.Length; iAttack++) {
    1155. varattackHit : RaycastHit = attackHits[iAttack];
    1156. if (!attackHit.collider.gameObject.transform.parent.gameObject.GetComponent(playerScript).isDead && !hitPlayer)
    1157. {
    1158. hitPlayer = true;
    1159. ComputeDamage(attackHit.collider.gameObject.transform.parent.gameObject);
    1160. }
    1161. }
    1162. }
    1163.  
    1164. functionDidDie()
    1165. {
    1166. HideEnemy();
    1167. //GiveQuestItem
    1168. if (transform.parent.GetComponent(enemyControl).questObject != null)
    1169. {
    1170. varnewQuestObject = Instantiate(transform.parent.GetComponent(enemyControl).questObject, Vector3(0,0,0), Quaternion.identity);
    1171. newQuestObject.transform.parent = questDirectory.transform;
    1172. newQuestObject.name = newQuestObject.name.Replace("(Clone)", "");
    1173. if (newQuestObject.GetComponent(questObject).displayInMenu)
    1174. {
    1175. vartempName = newQuestObject.name.Replace("QuestObject", "");
    1176. levelControl.GetComponent(startPosition).PostMessage("Got" + tempName + "!");
    1177. }
    1178. if (questItemAudio)
    1179. audio.PlayClipAtPoint(questItemAudio, transform.position);
    1180. }
    1181. isDead = true;
    1182. transform.parent.GetComponent(enemyControl).DidDie();
    1183. if (GetComponent(CapsuleCollider))
    1184. GetComponent(CapsuleCollider).enabled = false;
    1185. charNav.enabled = false;
    1186. charAnimator.speed = 1;
    1187. rigidbody.constraints = RigidbodyConstraints.FreezeAll;
    1188. vardidKillName = gameObject.name.Replace("Control", "Model");
    1189. didKillName = didKillName.Replace("(Clone)", "");
    1190. PlayerPrefs.SetInt("Killed" + didKillName, 1);
    1191. checkAttackHit = false;
    1192. gameObject.layer = LayerMask.NameToLayer("EnemyDead");
    1193. charNav.enabled = false;
    1194. playerObject.GetComponent(playerScript).GiveExperience(expPoints);
    1195. if (deathSound.Length != 0)
    1196. {
    1197. vardeathPlay : int = Random.Range(0, deathSound.Length);
    1198. audio.PlayClipAtPoint(deathSound[deathPlay], transform.position);
    1199. }
    1200. }
    1201.  
    1202. functionOnBecameVisible() {
    1203. isVisible = true;
    1204. }
    1205.  
    1206. functionOnBecameInvisible() {
    1207. isVisible = false;
    1208. }
    1209.  
    1210. functionUpdateStats(){
    1211. ResetStats();
    1212. for (varchildObject : Transformintransform){
    1213. if (childObject.tag == "StatModification"){
    1214. if (!childObject.gameObject.GetComponent(statModificationControl).ignoreThis)
    1215. {
    1216. modMight = modMight + childObject.gameObject.GetComponent(statModificationControl).modMightNumber;
    1217. modStamina = modStamina + childObject.gameObject.GetComponent(statModificationControl).modStaminaNumber;
    1218. modWisdom = modWisdom + childObject.gameObject.GetComponent(statModificationControl).modWisdomNumber;
    1219. modAccuracy = modAccuracy + childObject.gameObject.GetComponent(statModificationControl).modAccuracyNumber;
    1220. modSpeed = modSpeed + childObject.gameObject.GetComponent(statModificationControl).modSpeedNumber;
    1221. modLuck = modLuck + childObject.gameObject.GetComponent(statModificationControl).modLuckNumber;
    1222. modArmor = modArmor + childObject.gameObject.GetComponent(statModificationControl).modArmorNumber;
    1223. mightMod = mightMod + childObject.gameObject.GetComponent(statModificationControl).modMightPercent;
    1224. staminaMod = staminaMod + childObject.gameObject.GetComponent(statModificationControl).modStaminaPercent;
    1225. wisdomMod = wisdomMod + childObject.gameObject.GetComponent(statModificationControl).modWisdomPercent;
    1226. accuracyMod = accuracyMod + childObject.gameObject.GetComponent(statModificationControl).modAccuracyPercent;
    1227. speedMod = speedMod + childObject.gameObject.GetComponent(statModificationControl).modSpeedPercent;
    1228. luckMod = luckMod + childObject.gameObject.GetComponent(statModificationControl).modLuckPercent;
    1229. armorMod = armorMod + childObject.gameObject.GetComponent(statModificationControl).modArmorPercent;
    1230. if (resFireMod != 0)
    1231. resFireMod = resFireMod + (childObject.gameObject.GetComponent(statModificationControl).modFirePercent - 1);
    1232. else
    1233. resFireMod = resFireMod + childObject.gameObject.GetComponent(statModificationControl).modFirePercent;
    1234. if (resIceMod != 0)
    1235. resIceMod = resIceMod + (childObject.gameObject.GetComponent(statModificationControl).modIcePercent - 1);
    1236. else
    1237. resIceMod = resIceMod + childObject.gameObject.GetComponent(statModificationControl).modIcePercent;
    1238. if (resMagicMod != 0)
    1239. resMagicMod = resMagicMod + (childObject.gameObject.GetComponent(statModificationControl).modMagicPercent - 1);
    1240. else
    1241. resMagicMod = resMagicMod + childObject.gameObject.GetComponent(statModificationControl).modMagicPercent;
    1242. if (resPhysicalMod != 0)
    1243. resPhysicalMod = resPhysicalMod + (childObject.gameObject.GetComponent(statModificationControl).modPhysicalPercent - 1);
    1244. else
    1245. resPhysicalMod = resPhysicalMod + childObject.gameObject.GetComponent(statModificationControl).modPhysicalPercent;
    1246. moveSpeedMod = moveSpeedMod + childObject.gameObject.GetComponent(statModificationControl).modMovementPercent;
    1247. charNav.speed = moveSpeedMod;
    1248. if (!legacyAnimation)
    1249. charAnimator.speed = moveSpeedMod;
    1250. }
    1251. }
    1252. }
    1253. }
    1254.  
    1255. functionResetStats(){
    1256. modMight = 0;
    1257. modStamina = 0;
    1258. modWisdom = 0;
    1259. modAccuracy = 0;
    1260. modSpeed = 0;
    1261. modLuck = 0;
    1262. modArmor = 0;
    1263. mightMod = 1;
    1264. staminaMod = 1;
    1265. wisdomMod = 1;
    1266. accuracyMod = 1;
    1267. speedMod = 1;
    1268. luckMod = 1;
    1269. armorMod = 1;
    1270. resFireMod = 1;
    1271. resIceMod = 1;
    1272. resMagicMod = 1;
    1273. resPhysicalMod = 1;
    1274. moveSpeedMod = 1;
    1275. charNav.speed = moveSpeedMod;
    1276. if (!legacyAnimation)
    1277. charAnimator.speed = moveSpeedMod;
    1278. }
    1279.  
    1280. functionSetDestHere(){
    1281. if (charNav.enabled)
    1282. charNav.SetDestination(transform.position);
    1283. else
    1284. {
    1285. charNav.enabled = true;
    1286. charNav.SetDestination(transform.position);
    1287. charNav.enabled = false;
    1288. }
    1289. charAnimator.SetFloat("velocity", 0);
    1290. }
    1291.  
    1292. functionCastOnDeck(){
    1293. SetDestHere();
    1294. if (spellOnDeck.GetComponent(particleControlEnemy).targetsPlayer)
    1295. {
    1296. if (spellOnDeck.GetComponent(particleControlEnemy).shootsPlayer)
    1297. {
    1298. //InstantiateParticleinfrontofenemy
    1299. varnewForwardParticle = Instantiate(spellOnDeck, transform.position, Quaternion.identity);
    1300. varnewForwardForward = transform.position + (transform.TransformDirection (Vector3.forward));
    1301. newForwardParticle.transform.LookAt(newForwardForward);
    1302. newForwardParticle.GetComponent(particleControlEnemy).parentEnemy = gameObject;
    1303. if (newForwardParticle.GetComponent(particleControlEnemy).followPlayer)
    1304. newForwardParticle.GetComponent(particleControlEnemy).targetObject = playerModel;
    1305. //Audioonplayer
    1306. if (newForwardParticle.GetComponent(particleControlEnemy).audioToPlay)
    1307. {
    1308. //if (!newForwardParticle.GetComponent(particleControlEnemy).audioLoop)
    1309. audio.PlayClipAtPoint(newForwardParticle.GetComponent(particleControlEnemy).audioToPlay, transform.position);
    1310. }
    1311. }
    1312. else
    1313. {
    1314. //InstantiateonPlayerPosition
    1315. varnewPositionParticle = Instantiate(spellOnDeck, playerModel.transform.position, Quaternion.identity);
    1316. newPositionParticle.GetComponent(particleControlEnemy).parentEnemy = gameObject;
    1317. if (newPositionParticle.GetComponent(particleControlEnemy).followPlayer)
    1318. newPositionParticle.transform.parent = playerModel.transform;
    1319. //Audioonplayer
    1320. if (newPositionParticle.GetComponent(particleControlEnemy).audioToPlay)
    1321. audio.PlayClipAtPoint(newPositionParticle.GetComponent(particleControlEnemy).audioToPlay, playerModel.transform.position);
    1322. }
    1323. }
    1324. else
    1325. {
    1326. //Spellaffectsenemyand/orfriendlyenemy
    1327. vardamageTarget = this.gameObject;
    1328.  
    1329. if (spellOnDeck.GetComponent(particleControlEnemy).targetsSelf)
    1330. {
    1331. damageTarget = gameObject;
    1332. }
    1333. else
    1334. {
    1335. varfindOtherEnemies = Physics.OverlapSphere(playerModel.transform.position, 10);
    1336. damageTarget = this.gameObject;
    1337. varcycleCount = 0;
    1338. while (cycleCount < findOtherEnemies.Length && damageTarget == this.gameObject)
    1339. {
    1340. cycleCount += 1;
    1341.  
    1342. vartryThisEnemy : GameObject = findOtherEnemies[Random.Range(0, findOtherEnemies.Length)].collider.gameObject;
    1343. if (tryThisEnemy.tag == "EnemyObject")
    1344. {
    1345. if (tryThisEnemy.GetComponent(enemyScriptNew))
    1346. {
    1347. if (!tryThisEnemy.GetComponent(enemyScriptNew).isDead)
    1348. {
    1349. damageTarget = tryThisEnemy.collider.gameObject;
    1350. for (varchildObject : TransformintryThisEnemy.transform){
    1351. if (childObject.name == "StatMod" + spellOnDeck.name)
    1352. {
    1353. childObject.gameObject.GetComponent(statModificationControl).ignoreThis = true;
    1354. Destroy(childObject.gameObject);
    1355. break;
    1356. }
    1357. }
    1358. }
    1359. }
    1360. elseif (tryThisEnemy.GetComponent(enemyModelBasilisk))
    1361. {
    1362. if (!tryThisEnemy.GetComponent(enemyModelBasilisk).isDead)
    1363. {
    1364. damageTarget = tryThisEnemy.collider.gameObject;
    1365. for (varchildObject : TransformintryThisEnemy.transform){
    1366. if (childObject.name == "StatMod" + spellOnDeck.name)
    1367. {
    1368. childObject.gameObject.GetComponent(statModificationControl).ignoreThis = true;
    1369. Destroy(childObject.gameObject);
    1370. break;
    1371. }
    1372. }
    1373. }
    1374. }
    1375. }
    1376. }
    1377. }
    1378. varnewSelfParticle = Instantiate(spellOnDeck, damageTarget.transform.position, Quaternion.identity);
    1379. newSelfParticle.GetComponent(particleControlEnemy).parentEnemy = gameObject;
    1380. if (spellOnDeck.GetComponent(particleControlEnemy).followPlayer)
    1381. newSelfParticle.transform.parent = damageTarget.transform;
    1382. varnewSelfModification = Instantiate(spellOnDeck.GetComponent(particleControlEnemy).modificationPrefab, Vector3(0,0,0), Quaternion.identity);
    1383. newSelfModification.transform.parent = damageTarget.transform;
    1384. //newSelfModification.transform.eulerAngles = particleObjects[spellLevel - 1].GetComponent(particleControl).startRotationMod;
    1385. newSelfModification.transform.localEulerAngles = Vector3(0,0,0);
    1386. newSelfModification.name = "StatMod" + spellOnDeck.name;
    1387. newSelfModification.tag = "StatModification";
    1388. FillModifications(newSelfModification, damageTarget);
    1389. }
    1390. }
    1391.  
    1392. functionFillModifications(modificationObject : GameObject, targetObject : GameObject){
    1393. modificationObject.GetComponent(statModificationControl).destroyGameTime = levelControl.GetComponent(startPosition).gameTime + spellOnDeck.GetComponent(particleControlEnemy).modTimeLimit;
    1394.  
    1395. modificationObject.GetComponent(statModificationControl).modMightNumber = spellOnDeck.GetComponent(particleControlEnemy).modMightNumber;
    1396. modificationObject.GetComponent(statModificationControl).modStaminaNumber = spellOnDeck.GetComponent(particleControlEnemy).modStaminaNumber;
    1397. modificationObject.GetComponent(statModificationControl).modWisdomNumber = spellOnDeck.GetComponent(particleControlEnemy).modWisdomNumber;
    1398. modificationObject.GetComponent(statModificationControl).modAccuracyNumber = spellOnDeck.GetComponent(particleControlEnemy).modAccuracyNumber;
    1399. modificationObject.GetComponent(statModificationControl).modSpeedNumber = spellOnDeck.GetComponent(particleControlEnemy).modSpeedNumber;
    1400. modificationObject.GetComponent(statModificationControl).modLuckNumber = spellOnDeck.GetComponent(particleControlEnemy).modLuckNumber;
    1401. modificationObject.GetComponent(statModificationControl).modArmorNumber = spellOnDeck.GetComponent(particleControlEnemy).modArmorNumber;
    1402.  
    1403. //Chancesinpercent
    1404. modificationObject.GetComponent(statModificationControl).modMightPercent = spellOnDeck.GetComponent(particleControlEnemy).modMightPercent;
    1405. modificationObject.GetComponent(statModificationControl).modStaminaPercent = spellOnDeck.GetComponent(particleControlEnemy).modStaminaPercent;
    1406. modificationObject.GetComponent(statModificationControl).modWisdomPercent = spellOnDeck.GetComponent(particleControlEnemy).modWisdomPercent;
    1407. modificationObject.GetComponent(statModificationControl).modAccuracyPercent = spellOnDeck.GetComponent(particleControlEnemy).modAccuracyPercent;
    1408. modificationObject.GetComponent(statModificationControl).modSpeedPercent = spellOnDeck.GetComponent(particleControlEnemy).modSpeedPercent;
    1409. modificationObject.GetComponent(statModificationControl).modLuckPercent = spellOnDeck.GetComponent(particleControlEnemy).modLuckPercent;
    1410. modificationObject.GetComponent(statModificationControl).modArmorPercent = spellOnDeck.GetComponent(particleControlEnemy).modArmorPercent;
    1411.  
    1412. //Changesinpercent
    1413. modificationObject.GetComponent(statModificationControl).modMovementPercent = spellOnDeck.GetComponent(particleControlEnemy).modMovementPercent;
    1414. modificationObject.GetComponent(statModificationControl).modFirePercent = spellOnDeck.GetComponent(particleControlEnemy).modFirePercent;
    1415. modificationObject.GetComponent(statModificationControl).modIcePercent = spellOnDeck.GetComponent(particleControlEnemy).modIcePercent;
    1416. modificationObject.GetComponent(statModificationControl).modPhysicalPercent = spellOnDeck.GetComponent(particleControlEnemy).modPhysicalPercent;
    1417. modificationObject.GetComponent(statModificationControl).modMagicPercent = spellOnDeck.GetComponent(particleControlEnemy).modMagicPercent;
    1418.  
    1419. targetObject.GetComponent(enemyScriptNew).UpdateStats();
    1420. }
    1421.  
    1422. functionRightFoot(){
    1423.  
    1424. }
    1425.  
    1426. functionLeftFoot(){
    1427.  
    1428. }
     
    Last edited: Aug 17, 2014
  17. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    I dont have such code ;)../steals yours.. jks.

    Theres a lot of code there which i skimmed through.. didnt really see any obvious phsyics problems. Not sure how the navmesh agent works with physics thoug. I have a tendency to do all my own movement, only using the navmeshagent for getting the path.

    However, I did see plenty of code that could be improved..

    Every time you use a "string" you allocation a bit of garbage. You can avoid this by declaring some const strings instead. Personally I do it like so...

    Code (csharp):
    1.  
    2.  
    3. public class Functions
    4. {
    5.   public const string GETTITLECONTROLLER = "GetTitleController";
    6. }
    7.  
    8. public class Tags
    9. {
    10.   public const string ENEMYBLOCKONLY = "EnemyBlockOnly";
    11. }
    12.  
    13.  
    Then use them in you code...

    Code (csharp):
    1.  
    2.  
    3. void SomeFunction()
    4. {
    5. if (!IsInvoking(Functions.GETTITLECONTROLLER)
    6.    InvokeRepeating(Functions.GETTITLECONTROLLER, 0, 0.2);
    7. }
    8.  
    9.  
    theres more problems, but ill come back later... this pc im using needs to be rebooted.
     
  18. infinitypbr

    infinitypbr

    Joined:
    Nov 28, 2012
    Posts:
    3,149
    Thanks -- every little bit helps! I'm getting it almost always at or better than 30fps now, having removed much of the colliders on the skeleton enemies -- now they have their main capsule collider and only one hit box on their attack hand, for use when they're stretched out in an attack and the player hits them. I'll be doing the same to all the other enemies in the game.

    I also found that there were colliders on some magic spells that didn't need it, and that helped a bit.

    Now I'm just trying to hammer out all the smaller issues i"m finding -- like one about GUI.Repaint taking up extra ms when there are two enemy health bards being drawn at the same time.

    But the enemy script can certainly be improved, and garbage collection has been a bit of an issue as it periodically lowers a single frames rate.
     
  19. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,716
  20. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    Ok, some more things to fix (oh ffs this pos pc im using atm is still lagging when I try reply.. typing a reply with a 1 second delay on every key I hit is quite difficult.. seems its specific to this website though..)

    Here are some things you could improve..

    Use CompareTag rather than if transform.tag == ""
    Replace the following with one reference..

    Code (csharp):
    1.  
    2. [LIST=1]
    3. [*]castForwardParticle.GetComponent(particleScript).wisdom = (wisdomBase * wisdomMod);
    4. [*]castForwardParticle.GetComponent(particleScript).physical = physical;
    5. [*]castForwardParticle.GetComponent(particleScript).fire = fire;
    6. [*]castForwardParticle.GetComponent(particleScript).ice = ice;
    7. [*]castForwardParticle.GetComponent(particleScript).magic = magic;
    8. [/LIST]
    9.  
    10. var pScript = castForwardParticle.GetComponent(particleScript)
    11. pScript.wisdom = ...;
    12. pScript.physical = ...;
    13.  
    14.  
    I might take another look later when im on a decent pc
     
  21. Vanamerax

    Vanamerax

    Joined:
    Jan 12, 2012
    Posts:
    937
    Yep, caching all the Getcomponent calls should help some, as you are calling to them quite a lot. As a side effect, it also makes the code cleaner, so it would certainly be recommended to do.
     
  22. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Disable as much of the collision matrix as you can. There are gotchas though. OverlapSphere will look to that matrix regardless of any mask you pass into it. Raycast won't report back correctly for child colliders on a different layer than their parents unless interaction is turned on between the child and parent layer. You can try turning down the simulation timestep as well if the accuracy of the simulation isn't important to you.

    Also - any calls to Physics.OverlapSphere will profile under Physics.Simulate.
     
  23. infinitypbr

    infinitypbr

    Joined:
    Nov 28, 2012
    Posts:
    3,149
    Is there a better/faster way to get all enemies (or hit boxes) within a certain distance of a position, instead of using Physics.OverlapSphere?

    I call this a couple times on my player script when searching for enemies that I hit:

    Code (csharp):
    1. varfindLiveEnemies = Physics.OverlapSphere(playerModel.transform.position, 3, 1 << LayerMask.NameToLayer("Enemy"));
    I call it to search on layer "Enemy" and layer "Hit Box", and then from two positions, the base of the weapon and the tip. I haven't noticed any slow down with it ever, but if it goes into Physics.Simulate, then I suppose that could be part of it. I'll do a test right now, disabling those calls, and see what happens.
     
  24. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,716
    Unless you got a thousand enemies or call it a thousand times per frame, Physics.OverlapSphere cost almost nothing.
     
  25. infinitypbr

    infinitypbr

    Joined:
    Nov 28, 2012
    Posts:
    3,149
    Ah, ok. Neither :)
     
  26. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Not necessarily. Our OverlapSphere calls were more performant after disabling parts of the collision matrix. However - the bigger gains from that exercise were in the general physics simulation. So again - disable as much of it as you can but be cognizant of the fact that it will change the way some of your Physics.* calls behave.
     
  27. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    Yep, this is also a good tip. Refine your matrix to minimise un-nessesary collisions. (ie, my vehicle wheels can only collide with terrain).

    Rather than overlap sphere, I used a manager that each vehicle registered with, then did a simple distance check from a location when I wanted close vehciles. Works the same way as overlapsphere no doubt, but I have more control over what returns and how it returns... ie no new arrays created like overlap does
     
  28. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,716
    It really depend of what it is cast against. Against primitives, it's blazing fast.
     
  29. infinitypbr

    infinitypbr

    Joined:
    Nov 28, 2012
    Posts:
    3,149
    I didn't notice any real change when i just tested it. However I was still able to hit the enemies, which shouldn't have happened, so I need to double check things and test again when i get a chacne.
     
  30. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,716
    Just to be sure, you do have flagged everything in layers, and when you do Overlap or Raycast, you just perform it against the proper layers?
     
  31. infinitypbr

    infinitypbr

    Joined:
    Nov 28, 2012
    Posts:
    3,149
    Yep, true.
     
  32. infinitypbr

    infinitypbr

    Joined:
    Nov 28, 2012
    Posts:
    3,149
    I think this other thread I started for another issue I'm working out isn't getting traction -- any chance you have an idea about it? I need to test doing it a different way, I suppose, but I'm getting ready for Unite (flying up tomorrow), so I probably won't get a chance. And I'm not sure if I'm just missing something or not.

    http://forum.unity3d.com/threads/gu...s-are-drawn-draw-texture.262836/#post-1738134
     
  33. infinitypbr

    infinitypbr

    Joined:
    Nov 28, 2012
    Posts:
    3,149

    @JamesLeeNZ -- does the "public class" go into the same script, or on a different script that is somehow globally available to all other scripts?
     
  34. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    You can put it where ever you like..

    as long as its not inside a class, it will be global..

    simple way to do it.

    Create new script, call it Helper, or something similar..
    delete all code
    paste my code.
    done.
     
  35. infinitypbr

    infinitypbr

    Joined:
    Nov 28, 2012
    Posts:
    3,149
    Cool cool, I'll try to get to that tonight! Thanks!