Advertisement
Guest User

Untitled

a guest
Dec 13th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.24 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class PlayerController : MonoBehaviour
  6. {
  7. [Header ("MOVEMENT VALUES")]
  8. public float speed;
  9. private float moveInput;
  10. private Rigidbody2D rb;
  11.  
  12. [Header ("JUMP VALUES")]
  13.  
  14. public float jumpForce;
  15. public Transform GroundCheck;
  16. public float checkRadius;
  17. public LayerMask whatIsGround;
  18. private bool isJumping;
  19. private bool isGrounded;
  20.  
  21. [Header("ATTACK VALUES")]
  22. public float attackSpeed;
  23. public Vector2 maxAttackRange;
  24. private Transform characterPosition;
  25. private Vector2 targetPosition;
  26. private bool isAttacking = false;
  27. public bool canAttack;
  28.  
  29. [Header("DEBUGLINES VALUES")]
  30. public Color lineColor;
  31. public float lineDuration;
  32. public bool inDepth;
  33.  
  34. // Start is called before the first frame update
  35. void Start()
  36. {
  37. rb = GetComponent<Rigidbody2D>();
  38. }
  39.  
  40. //Must handle every physics calculation in the game
  41. private void FixedUpdate()
  42. {
  43. isGrounded = Physics2D.OverlapCircle(GroundCheck.position, checkRadius, whatIsGround);
  44. moveInput = Input.GetAxisRaw("Horizontal");
  45. rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
  46. }
  47.  
  48. // Update is called once per frame
  49. void Update()
  50. {
  51.  
  52. //jump
  53. if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
  54. {
  55. rb.velocity = Vector2.up * jumpForce;
  56. }
  57.  
  58. //1 solo frame
  59. if (Input.GetButtonUp("Fire1"))
  60. {
  61. isAttacking = true;
  62. targetPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
  63. StartCoroutine(AttackTo(transform.position, targetPosition, attackSpeed));
  64.  
  65. Debug.DrawLine(transform.position, targetPosition, lineColor, lineDuration, false);
  66. }
  67. }
  68.  
  69. IEnumerator AttackTo(Vector2 currentPosition, Vector2 targetPosition, float speed)
  70. {
  71. while (isAttacking)
  72. {
  73. transform.position = Vector2.MoveTowards(currentPosition, targetPosition,speed * Time.deltaTime);
  74. if(Vector2.Distance(transform.position, targetPosition.position) < 5)
  75. {
  76. isAttacking = false;
  77. }
  78. }
  79. yield return null;
  80. }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement