Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- playercontroller code:
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class PlayerController : MonoBehaviour
- {
- public Animator camAnim;
- public Animator playerAnim;
- private Rigidbody2D rb;
- public float speed;
- public float moveInput;
- public float jumpForce;
- private bool isGrounded;
- public Transform feetPos;
- public float checkRadius;
- public LayerMask whatIsGround;
- private float jumpTimeCounter;
- public float jumpTime;
- private bool isJumping;
- private float timeBtwAttack;
- public float starTimeBtwAttack;
- public Transform attackPos;
- public LayerMask whatIsEnemies;
- public float attackRange;
- public int damage;
- // Start is called before the first frame update
- void Start()
- {
- rb = GetComponent<Rigidbody2D>();
- }
- // Update is called once per physics update
- private void FixedUpdate()
- {
- playerAnim = GetComponent<Animator>();
- moveInput = Input.GetAxisRaw("PlayerHorizontal");
- rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
- }
- // Update is called once per frame
- void Update()
- {
- isGrounded = Physics2D.OverlapCircle(feetPos.position, checkRadius, whatIsGround);
- if(moveInput > 0)
- {
- transform.eulerAngles = new Vector3(0, 0, 0);
- }
- else if(moveInput < 0)
- {
- transform.eulerAngles = new Vector3(0, 180, 0);
- }
- if(isGrounded == true && Input.GetKeyDown(KeyCode.Space))
- {
- isJumping = true;
- jumpTimeCounter = jumpTime;
- rb.velocity = Vector2.up * jumpForce;
- }
- if(Input.GetKey(KeyCode.Space) && isJumping == true)
- {
- if(jumpTimeCounter > 0)
- {
- rb.velocity = Vector2.up * jumpForce;
- jumpTimeCounter -= Time.deltaTime;
- }
- }
- if(Input.GetKeyUp(KeyCode.Space))
- {
- isJumping = false;
- }
- //---------------------------------------------------------------------------------------------------------
- if(timeBtwAttack <= 0)
- {
- if(Input.GetKey(KeyCode.J))
- {
- //camAnim.SetTrigger("shake");
- playerAnim.SetBool("attack", true);
- Collider2D[] enemiesToDamage = Physics2D.OverlapCircleAll(attackPos.position, attackRange, whatIsEnemies);
- for (int i = 0; i < enemiesToDamage.Length; i++)
- {
- enemiesToDamage[i].GetComponent<Enemy>().TakeDamage(damage);
- playerAnim.SetTrigger("attack");
- }
- }
- else
- {
- playerAnim.SetBool("attack", false);
- }
- timeBtwAttack = starTimeBtwAttack;
- }
- else
- {
- timeBtwAttack -= Time.deltaTime;
- }
- }
- void OnDrawGizmosSelected()
- {
- Gizmos.color = Color.red;
- Gizmos.DrawWireSphere(attackPos.position, attackRange);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement