/Project/Assets/Scripts/New C# scripts/Player1attributes.cs

https://bitbucket.org/golivegaming/lalkamal-neelkamal · C# · 78 lines · 61 code · 6 blank · 11 comment · 11 complexity · 646e1a41b94cda708f94a182d282aee9 MD5 · raw file

  1. /*..................
  2. * TO : Assign attributes to the player2
  3. * walk
  4. * Attack
  5. * Enemy spawn
  6. ...............*/
  7. using UnityEngine;
  8. using System.Collections;
  9. public class Player1attributes : MonoBehaviour
  10. {
  11. public Rigidbody HeroBullet;
  12. public Rigidbody[] EnemyPrefab = new Rigidbody[3];
  13. Animation HeroAnimation;
  14. static public int PlayFire = 0;
  15. void Update ()
  16. {
  17. if(!GlobalAttributes.isPaused)
  18. {
  19. HeroAnimation = gameObject.GetComponent("Animation") as Animation;
  20. //give movement to the hero
  21. this.gameObject.transform.Translate(Vector3.forward *Time.smoothDeltaTime * GlobalAttributes.HeroWalkSpeed);
  22. // Initlize enemy at particular distance from hero
  23. if(GlobalAttributes.EnemyLife == 0)
  24. {
  25. Rigidbody clone = (Rigidbody)Instantiate(EnemyPrefab[Random.Range(0, EnemyPrefab.Length)], transform.FindChild("Enemyspawn position").transform.position, transform.FindChild("Enemyspawn position").transform.rotation);
  26. GlobalAttributes.EnemyLife = 1;
  27. }
  28. // Fire from hero seeing towards target
  29. if(GlobalAttributes.HeroFireFlag == 0)
  30. {
  31. LookAt(transform.FindChild("Fireposition").gameObject, GameObject.FindWithTag("enemy").transform.FindChild("Bip001").gameObject, GlobalAttributes.LookSpeed);
  32. FireAt(HeroBullet, transform.FindChild("Fireposition").gameObject, GlobalAttributes.HeroFireSpeed);
  33. GlobalAttributes.HeroFireFlag = 1;
  34. }
  35. if(DistanceStop.HeroDistance < GlobalAttributes.HeroRange)
  36. {
  37. HeroAnimation.CrossFade("attackhero1");
  38. }
  39. else
  40. HeroAnimation.CrossFade("runhero1");
  41. }
  42. }
  43. // To turn according to the target
  44. void LookAt(GameObject obj, GameObject targetobj, float rotationspeed)
  45. {
  46. Quaternion rotation = Quaternion.LookRotation(targetobj.transform.position - obj.transform.position);
  47. obj.transform.rotation = Quaternion.Slerp(obj.transform.rotation, rotation,rotationspeed);
  48. }
  49. // To fire towards the target
  50. void FireAt(Rigidbody projectile, GameObject obj, float firespeed)
  51. {
  52. Rigidbody clone = (Rigidbody) Instantiate(projectile, obj.transform.position, obj.transform.rotation);
  53. clone.velocity = obj.transform.forward * firespeed;
  54. }
  55. void OnCollisionEnter(Collision collision)
  56. {
  57. if(!GlobalAttributes.isPaused)
  58. {
  59. if(collision.gameObject.tag == "enemybullet")
  60. {
  61. GlobalAttributes.HeroHealth -= GlobalAttributes.EnemyStrength;
  62. Destroy(collision.gameObject);
  63. }
  64. if(GlobalAttributes.HeroHealth == 0)
  65. {
  66. Destroy(this.gameObject);
  67. Destroy(GameObject.FindWithTag("hero2"));
  68. Destroy(GameObject.FindWithTag("enemy"));
  69. }
  70. }
  71. }
  72. }