PlayerController.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using UnityEngine;
  2. using System.Collections;
  3. public class PlayerController : MonoBehaviour {
  4. private Animation anim;
  5. public float turnSmoothing = 15f;
  6. public GameObject Bomb;
  7. public Transform myTransform;
  8. public float moveSpeed = 3f;
  9. public bool IsDead = false;
  10. public GameController gamecontroller;
  11. private Rigidbody rigidBody;
  12. public int playerNumber = 1;
  13. public int playerbomb = 2;
  14. public int lifenum = 2;
  15. void Awake () {
  16. anim = GetComponent <Animation>();
  17. myTransform = GetComponent<Transform> ();
  18. rigidBody = GetComponent<Rigidbody> ();
  19. }
  20. void Update () {
  21. }
  22. void Rotating(float h,float v){
  23. Vector3 targetDir = new Vector3(h,0,v);
  24. Quaternion targetRotation = Quaternion.LookRotation (targetDir, Vector3.up);
  25. Rigidbody r = GetComponent<Rigidbody> ();
  26. Quaternion newRotation = Quaternion.Lerp (r.rotation, targetRotation, turnSmoothing * Time.deltaTime);
  27. r.MoveRotation (newRotation);
  28. }
  29. void MoveManagement(float h,float v){
  30. if (h != 0 || v != 0) {
  31. Rotating (h,v);
  32. Vector3 speed = new Vector3(h,0,v);
  33. rigidBody.MovePosition (rigidBody.position + moveSpeed * speed * Time.deltaTime);
  34. }
  35. }
  36. void FixedUpdate(){
  37. if (playerNumber == 1) {
  38. float h = Input.GetAxis ("Horizontal");
  39. float v = Input.GetAxis ("Vertical");
  40. anim.Play("ghost_idle_hover");
  41. MoveManagement (h, v);
  42. if (Input.GetKeyDown ("f")) {
  43. DropBomb();
  44. anim.Play("ghost_attack_with_ball");
  45. }
  46. }
  47. else
  48. {
  49. float h = Input.GetAxis ("Fire1");
  50. float v = Input.GetAxis ("Fire2");
  51. anim.Play("dragon_dizzy");
  52. MoveManagement (h, v);
  53. if (Input.GetKeyDown ("l")) {
  54. DropBomb();
  55. anim.Play("dragon_bite");
  56. }
  57. }
  58. }
  59. private void DropBomb() {
  60. if (Bomb) {
  61. Bomb.GetComponent<Bomb> ().explosionLength = playerbomb;
  62. Vector3 BombPos = new Vector3 (Mathf.RoundToInt (myTransform.position.x),
  63. 0, Mathf.RoundToInt (myTransform.position.z));
  64. Instantiate(Bomb, BombPos,Bomb.transform.rotation);
  65. }
  66. }
  67. public void OnTriggerEnter(Collider other) {
  68. if (other.CompareTag ("Explosion")) {
  69. if (lifenum == 1) {
  70. lifenum = lifenum - 1;
  71. Debug.Log ("P" + playerNumber + "hit by explosion!");
  72. IsDead = true;
  73. gamecontroller.playerDied (playerNumber);
  74. Destroy (gameObject);
  75. } else {
  76. lifenum = lifenum - 1;
  77. }
  78. }
  79. if (other.CompareTag ("BombLength")) {
  80. playerbomb = playerbomb + 1;
  81. }
  82. if (other.CompareTag ("lifecube")) {
  83. lifenum = lifenum + 1;
  84. }
  85. if (other.CompareTag ("SpeedCube")) {
  86. moveSpeed = moveSpeed + 0.5f;
  87. }
  88. }
  89. }