[ create a new paste ] login | about

Link: http://codepad.org/hQfWf38R    [ raw code | fork ]

C, pasted on Oct 13:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Player_Controller : MonoBehaviour
{
    public float speed, jump;
    public float shootSpeed, slashSpeed;
    public int hp;
    public Image hp_UI, back_hp_UI;
    public Animator blood_Splat, land_Effect;
    public GameObject hp_Canvas;
    public Running_Dust runningDust;
    public Transform spawnBulletPoint;
    public Bullet bullet;

    private List<Bullet> bulletsReady = new List<Bullet>();
    private bool land;
    private bool isDead;
    private int current_hp;
    private float height;
    private int layerFloor;
    private Rigidbody2D rb;
    private Animator anim;
    private bool canShoot = true, canSlash = true;

    private void Awake()
        {
        rb = GetComponent<Rigidbody2D>();
        Collider2D coll = GetComponent<Collider2D>();
        height = coll.bounds.extents.y + 0.05f;
        layerFloor = 1 << LayerMask.NameToLayer("Floor");
        anim = GetComponent<Animator>();
        }

    private void Start()
        {
        current_hp = hp;
        hp_UI.fillAmount = 1;
        }

    private void FixedUpdate()
        {
        LandEffect();
        anim.SetBool("isGround", isGround());
        Move();
        if(input().y > 0 && isGround())
            rb.velocity = new Vector2(rb.velocity.x, jump);
        Attacks();
        }

    void LandEffect ()
        {
        if(!isGround()) land = false;
        if(isGround() && !land && rb.velocity.y < 0)
            {            
            land_Effect.transform.position = playerPos();
            land_Effect.SetTrigger("Land");
            land = true;
            }
        }

    public void InstantiateDust ()
        {
        if(rb.velocity.y == 0)
            runningDust.SpawnDust();
        }

    void Move ()
        {
        bool running = (input().x != 0) ? true : false;
        anim.SetBool("Running", running);
        if(isGround())
            rb.velocity = new Vector2(input().x * speed, rb.velocity.y);
        else
            rb.velocity = new Vector2(input().x * speed * 0.75f, rb.velocity.y); 
        int dir = (int)input().x;
        if (dir != 0)
            {
            transform.localScale = new Vector3(dir, 1, 1);         
            hp_Canvas.transform.localScale = new Vector3(dir, 1, 1);
            int rot = (dir == 1) ? 0 : 180;
            spawnBulletPoint.transform.localEulerAngles = new Vector3(0, rot, 0);
            }
        }

    void Attacks ()
        {
        if(Input.GetMouseButtonDown(0) && canShoot)
            {
            canShoot = false;
            canSlash = false;
            StartCoroutine(Shoot());
            }
        if(Input.GetMouseButtonDown(1) && canSlash)
            {
            canShoot = false;
            canSlash = false;
            StartCoroutine(Slash());
            }
        }

    private void OnTriggerEnter2D(Collider2D other)
        {
        if(other.CompareTag("Explosion"))
            {
            if(isDead)
                return;
            other.isTrigger = false;
            if(hitted != null)
                StopCoroutine(hitted);
            hitted = StartCoroutine(Hitted());
            }
        }

    private Coroutine hitted;
    IEnumerator Hitted ()
        {
        blood_Splat.SetTrigger("Hurt");
        hp_UI.color = new Color(0.05f,0.73f,0,1);
        back_hp_UI.color = new Color(1,0,0,1);
        current_hp--;
        if(current_hp <= 0)
            {
            //Die();
            }
        hp_UI.fillAmount = (float)current_hp/hp;
        yield return new WaitForSeconds(1);
        float alpha = 1;
        while(alpha > 0.5f)
            {
            hp_UI.color = new Color(0.05f,0.73f,0,alpha);
            back_hp_UI.color = new Color(1,0,0,alpha);
            alpha -= 0.05f;
            yield return new WaitForSeconds(0.1f);
            }
        }

    IEnumerator Shoot ()
        {
        //Instantiate  Bullet
        anim.SetTrigger("Shoot");
        yield return new WaitForSeconds(shootSpeed);
        canShoot = true;
        canSlash = true;
        }

    IEnumerator Slash ()
        {
        //Instantiate  slash effect
        //check colliders
        //Do damage
        anim.SetTrigger("Slash");
        yield return new WaitForSeconds(slashSpeed);
        canShoot = true;
        canSlash = true;
        }

    Vector2 playerPos (float xFix = 0)
        {
        return new Vector2(transform.position.x + xFix, transform.position.y - height);
        }

    public bool isGround ()
        {
        bool bottom1 = Physics2D.Raycast(playerPos(), -Vector2.up, 0.2f, layerFloor);
        bool bottom2 = Physics2D.Raycast(playerPos(0.4f), -Vector2.up, 0.2f, layerFloor);
        bool bottom3 = Physics2D.Raycast(playerPos(-0.4f), -Vector2.up, 0.2f, layerFloor);
        Debug.DrawRay(playerPos(), -Vector2.up*0.2f, Color.red);
        Debug.DrawRay(playerPos(0.4f), -Vector2.up*0.2f, Color.red);
        Debug.DrawRay(playerPos(-0.4f), -Vector2.up*0.2f, Color.red);
        if(bottom1 || bottom2 || bottom3)
            return true;
        else
            return false;
        }

    public Vector2 input()
        {
        Vector2 _input = Vector2.zero;
        if(Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.A))
            {
            if(!Input.GetKey(KeyCode.RightArrow) && !Input.GetKey(KeyCode.D))
                _input.x = -1;
            }
        else if(Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.D))
            {
            if(!Input.GetKey(KeyCode.LeftArrow) && !Input.GetKey(KeyCode.A))
                _input.x = 1;
            }
        else
            _input.x = 0;

        if(Input.GetKeyDown(KeyCode.Space))
            _input.y = 1;
        
        return _input;
        }

    public void ShootBullet ()
        {
        if(bulletsReady.Count > 0)
            {
            Bullet bulletObj = bulletsReady[0];        
            bulletObj.transform.position = spawnBulletPoint.position;
            bulletObj.transform.rotation = spawnBulletPoint.rotation;
            bulletObj.transform.gameObject.SetActive(true);
            bulletsReady.Remove(bulletObj);
            }
        else
            {
            Bullet bulletObj = Instantiate(bullet, spawnBulletPoint.position, spawnBulletPoint.rotation) as Bullet;
            bulletObj.player = this;
            }
        }

    public void AddBulletToPool (Bullet b)
        {
        b.transform.gameObject.SetActive(false);
        bulletsReady.Add(b);
        }
}


Create a new paste based on this one


Comments: