Advertisement
Guest User

Untitled

a guest
Oct 18th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.17 KB | None | 0 0
  1. playercontroller code:
  2.  
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6.  
  7. public class PlayerController : MonoBehaviour
  8. {
  9. public Animator camAnim;
  10. public Animator playerAnim;
  11.  
  12. private Rigidbody2D rb;
  13. public float speed;
  14. public float moveInput;
  15. public float jumpForce;
  16. private bool isGrounded;
  17. public Transform feetPos;
  18. public float checkRadius;
  19. public LayerMask whatIsGround;
  20. private float jumpTimeCounter;
  21. public float jumpTime;
  22. private bool isJumping;
  23.  
  24. private float timeBtwAttack;
  25. public float starTimeBtwAttack;
  26.  
  27. public Transform attackPos;
  28. public LayerMask whatIsEnemies;
  29. public float attackRange;
  30. public int damage;
  31.  
  32. // Start is called before the first frame update
  33. void Start()
  34. {
  35. rb = GetComponent<Rigidbody2D>();
  36. }
  37.  
  38. // Update is called once per physics update
  39. private void FixedUpdate()
  40. {
  41. playerAnim = GetComponent<Animator>();
  42. moveInput = Input.GetAxisRaw("PlayerHorizontal");
  43. rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
  44. }
  45.  
  46. // Update is called once per frame
  47. void Update()
  48. {
  49. isGrounded = Physics2D.OverlapCircle(feetPos.position, checkRadius, whatIsGround);
  50.  
  51. if(moveInput > 0)
  52. {
  53. transform.eulerAngles = new Vector3(0, 0, 0);
  54. }
  55. else if(moveInput < 0)
  56. {
  57. transform.eulerAngles = new Vector3(0, 180, 0);
  58. }
  59.  
  60. if(isGrounded == true && Input.GetKeyDown(KeyCode.Space))
  61. {
  62. isJumping = true;
  63. jumpTimeCounter = jumpTime;
  64. rb.velocity = Vector2.up * jumpForce;
  65. }
  66.  
  67. if(Input.GetKey(KeyCode.Space) && isJumping == true)
  68. {
  69. if(jumpTimeCounter > 0)
  70. {
  71. rb.velocity = Vector2.up * jumpForce;
  72. jumpTimeCounter -= Time.deltaTime;
  73. }
  74.  
  75. }
  76.  
  77. if(Input.GetKeyUp(KeyCode.Space))
  78. {
  79. isJumping = false;
  80. }
  81.  
  82. //---------------------------------------------------------------------------------------------------------
  83.  
  84. if(timeBtwAttack <= 0)
  85. {
  86. if(Input.GetKey(KeyCode.J))
  87. {
  88. //camAnim.SetTrigger("shake");
  89. playerAnim.SetBool("attack", true);
  90. Collider2D[] enemiesToDamage = Physics2D.OverlapCircleAll(attackPos.position, attackRange, whatIsEnemies);
  91. for (int i = 0; i < enemiesToDamage.Length; i++)
  92. {
  93. enemiesToDamage[i].GetComponent<Enemy>().TakeDamage(damage);
  94. playerAnim.SetTrigger("attack");
  95. }
  96. }
  97. else
  98. {
  99. playerAnim.SetBool("attack", false);
  100. }
  101. timeBtwAttack = starTimeBtwAttack;
  102. }
  103. else
  104. {
  105. timeBtwAttack -= Time.deltaTime;
  106. }
  107. }
  108.  
  109. void OnDrawGizmosSelected()
  110. {
  111. Gizmos.color = Color.red;
  112. Gizmos.DrawWireSphere(attackPos.position, attackRange);
  113.  
  114. }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement