Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class PlayerController : MonoBehaviour
- {
- [Header ("MOVEMENT VALUES")]
- public float speed;
- private float moveInput;
- private Rigidbody2D rb;
- [Header ("JUMP VALUES")]
- public float jumpForce;
- public Transform GroundCheck;
- public float checkRadius;
- public LayerMask whatIsGround;
- private bool isJumping;
- private bool isGrounded;
- [Header("ATTACK VALUES")]
- public float attackSpeed;
- public Vector2 maxAttackRange;
- private Transform characterPosition;
- private Vector2 targetPosition;
- private bool isAttacking = false;
- public bool canAttack;
- [Header("DEBUGLINES VALUES")]
- public Color lineColor;
- public float lineDuration;
- public bool inDepth;
- // Start is called before the first frame update
- void Start()
- {
- rb = GetComponent<Rigidbody2D>();
- }
- //Must handle every physics calculation in the game
- private void FixedUpdate()
- {
- isGrounded = Physics2D.OverlapCircle(GroundCheck.position, checkRadius, whatIsGround);
- moveInput = Input.GetAxisRaw("Horizontal");
- rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
- }
- // Update is called once per frame
- void Update()
- {
- //jump
- if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
- {
- rb.velocity = Vector2.up * jumpForce;
- }
- //1 solo frame
- if (Input.GetButtonUp("Fire1"))
- {
- isAttacking = true;
- targetPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
- StartCoroutine(AttackTo(transform.position, targetPosition, attackSpeed));
- Debug.DrawLine(transform.position, targetPosition, lineColor, lineDuration, false);
- }
- }
- IEnumerator AttackTo(Vector2 currentPosition, Vector2 targetPosition, float speed)
- {
- while (isAttacking)
- {
- transform.position = Vector2.MoveTowards(currentPosition, targetPosition,speed * Time.deltaTime);
- if(Vector2.Distance(transform.position, targetPosition.position) < 5)
- {
- isAttacking = false;
- }
- }
- yield return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement