Advertisement
GoodNoodle

Player Controller

Sep 23rd, 2021
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.55 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5.  
  6. public enum PlayerState
  7. {
  8.     Walk,
  9.     Shoot,
  10.     Attack,
  11.     Intaract,
  12.     idle,
  13.     stagger
  14. }
  15. public class PlayerController : MonoBehaviour
  16. {
  17.     public Image staminaProgress = null;
  18.     public CanvasGroup slider = null;
  19.     public static PlayerController instance;
  20.     public PlayerState state;
  21.     public float sprintSpeed, sprintCost;
  22.     public float moveSpeed;
  23.     public Rigidbody2D theRB;
  24.     private Vector3 change;
  25.     private Animator anim;
  26.    
  27.  
  28.     public SpriteRenderer theSR;
  29.     public Sprite[] playerDir;
  30.  
  31.     /*
  32.     public FloatValue currentHealth;
  33.     public Signals healthSig;
  34.     */
  35.     public GameObject projectile;
  36.     public int currentAmmo;
  37.     // Start is called before the first frame update
  38.     private void Awake()
  39.     {
  40.         instance = this;
  41.     }
  42.  
  43.     void Start()
  44.     {
  45.         theRB = GetComponent<Rigidbody2D>();
  46.         anim = GetComponent<Animator>();
  47.        
  48.     }
  49.  
  50.     // Update is called once per frame
  51.     void Update()
  52.     {
  53.             MoveCharecter();
  54.             change = Vector3.zero;
  55.             change.x = Input.GetAxisRaw("Horizontal");
  56.             change.y = Input.GetAxisRaw("Vertical");
  57.             UpdateAnimAndMove();
  58.             if (Input.GetButtonDown("attack") && state != PlayerState.Attack && state != PlayerState.Shoot && state != PlayerState.stagger)
  59.         {
  60.                 StartCoroutine(AttackCo());
  61.             } else if (Input.GetButtonDown("shoot") && state != PlayerState.Attack && state != PlayerState.Shoot && state != PlayerState.stagger)
  62.         {
  63.             StartCoroutine(ShootCo());
  64.         }
  65.             else if (state == PlayerState.Walk || state == PlayerState.idle)
  66.             {
  67.                 UpdateAnimAndMove();
  68.             }
  69.        
  70.  
  71.     }
  72.  
  73.  
  74.  
  75.  
  76.  
  77.    
  78.  
  79.     void UpdateAnimAndMove()
  80.     {
  81.         if (change != Vector3.zero)
  82.         {
  83.             anim.SetFloat("MoveX", change.x);
  84.             anim.SetFloat("MoveY", change.y);
  85.             anim.SetBool("Move", true);
  86.         }
  87.         else
  88.         {
  89.             anim.SetBool("Move", false);
  90.         }
  91.     }
  92.     void MoveCharecter()
  93.     {
  94.         theRB.MovePosition(transform.position + change * moveSpeed * Time.fixedDeltaTime);
  95.     }
  96.    
  97.     private IEnumerator AttackCo()
  98.     {
  99.         anim.SetBool("Attack", true);
  100.         state = PlayerState.Attack;
  101.         yield return null;
  102.         anim.SetBool("Attack", false);
  103.         yield return  new WaitForSeconds(.33f);
  104.         state = PlayerState.Walk;
  105.     }
  106.  
  107.     private IEnumerator ShootCo()
  108.     {
  109.         if(currentAmmo <= 0)
  110.         {
  111.             currentAmmo = 0;
  112.         }
  113.         if (currentAmmo > 0)
  114.         {
  115.             anim.SetBool("shoot", true);
  116.             state = PlayerState.Attack;
  117.             yield return null;
  118.             MakeBullet();
  119.             anim.SetBool("shoot", false);
  120.             yield return new WaitForSeconds(.33f);
  121.             state = PlayerState.Walk;
  122.         }
  123.          currentAmmo--;
  124.     }
  125.  
  126.  
  127.  
  128.  
  129.     private void MakeBullet()
  130.     {
  131.         Vector2 temp = new Vector2(anim.GetFloat("MoveX"), anim.GetFloat("MoveY"));
  132.         Bullet bullet = Instantiate(projectile, transform.position, Quaternion.identity).GetComponent<Bullet>();
  133.         bullet.SetUp(temp, BulletDir());
  134.     }
  135.  
  136.     public Vector3 BulletDir()
  137.     {
  138.         float temp = Mathf.Atan2(anim.GetFloat("MoveY"), anim.GetFloat("MoveX") * Mathf.Rad2Deg);
  139.         return new Vector3(0, 0, temp);
  140.     }
  141.  
  142. }
  143.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement