Advertisement
Guest User

Untitled

a guest
Apr 25th, 2018
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.52 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class PlayerControllerAlva : MonoBehaviour {
  6.  
  7. public float speed = 10, jumpVelocity = 10;
  8. Transform myTrans, tagGround;
  9. Rigidbody2D myBody;
  10. public bool canMoveInAir = true;
  11. public LayerMask playerMask;
  12. private float hInput = 0;
  13. public bool facingRight;
  14. public float horizontal;
  15. AnimationPlayerController myAnim;
  16. public bool isGrounded = false;
  17. private bool defenseMode = false;
  18.  
  19. void Start()
  20. {
  21.  
  22. facingRight = true;
  23. myBody = this.GetComponent<Rigidbody2D>();
  24. myTrans = this.transform;
  25. tagGround = GameObject.Find(this.name + "/tag_ground").transform;
  26. //myAnim = AnimationPlayerController.instance;
  27. }
  28.  
  29.  
  30.  
  31.  
  32. void FixedUpdate()
  33. {
  34. isGrounded = Physics2D.Linecast(myTrans.position, tagGround.position, playerMask);
  35. //myAnim.UpdateIsGrounded(isGrounded);
  36. DefenseMode(defenseMode);
  37. Flip(horizontal);
  38.  
  39.  
  40.  
  41. horizontal = Input.GetAxis("Horizontal");
  42. #if !UNITY_ANDROID && !UNITY_IPHONE && !UNITY_BLACKBERRY && !UNITY_WINRT
  43. hInput = Input.GetAxisRaw("Horizontal");
  44. //myAnim.UpdateSpeed(hInput);
  45. if (Input.GetButtonDown("Jump"))
  46. {
  47. Jump();
  48. }
  49. #endif
  50. Move(hInput);
  51.  
  52. }
  53.  
  54. public void DefenseMode(bool mode)
  55. {
  56.  
  57. if (Input.GetKeyDown(KeyCode.LeftControl))
  58. {
  59. this.gameObject.GetComponent<Transform>().localScale = new Vector3(2, 1, 1);
  60. gameObject.GetComponent<PlayController>().speed = 3;
  61. gameObject.GetComponent<PlayController>().jumpVelocity = 3;
  62. gameObject.GetComponent<Shooting>().enabled = false;
  63. defenseMode = true;
  64. this.tag = "PlayerDefense";
  65. }
  66. if (Input.GetKeyUp(KeyCode.LeftControl))
  67. {
  68. this.gameObject.GetComponent<Transform>().localScale = new Vector3(1, 1, 1);
  69. gameObject.GetComponent<PlayController>().speed = 5;
  70. gameObject.GetComponent<PlayController>().jumpVelocity = 5;
  71. gameObject.GetComponent<Shooting>().enabled = true;
  72. defenseMode = false;
  73. this.tag = "PlayerAttack";
  74. }
  75. }
  76.  
  77. void Move(float horizontalInput)
  78. {
  79.  
  80. if (!canMoveInAir && !isGrounded)
  81. return;
  82. Vector2 moveVel = myBody.velocity;
  83. moveVel.x = horizontalInput * speed;
  84. myBody.velocity = moveVel;
  85. }
  86.  
  87. public void Jump()
  88. {
  89. if (isGrounded)
  90. {
  91. myBody.velocity += jumpVelocity * Vector2.up;
  92. }
  93. }
  94.  
  95. public void StartMoving(float horizontalInput)
  96. {
  97. hInput = horizontalInput;
  98. horizontal = horizontalInput;
  99. //myAnim.UpdateSpeed(horizontalInput);
  100. }
  101.  
  102. private void Flip(float horizontal)
  103. {
  104. if (horizontal > 0 && !facingRight || horizontal < 0 && facingRight)
  105. {
  106. facingRight = !facingRight;
  107. Vector3 theScale = transform.localScale;
  108.  
  109. theScale.x *= -1;
  110.  
  111. transform.localScale = theScale;
  112. }
  113. }
  114.  
  115.  
  116. //private void OnCollisionEnter2D(Collision2D collision)
  117. //{
  118. // if(collision.gameObject.tag == "Item")
  119. // {
  120. // GameManager.Instance.CollectedItems--;
  121. // GameManager.Instance.amountOfItemsNeeded--;
  122. // Destroy(collision.gameObject);
  123. // }
  124. //}
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement