Skip to content
Snippets Groups Projects
  • Jordan's avatar
    8f043592
    New character art and animations · 8f043592
    Jordan authored
    - Character is now sperated into seperate parts
    - All new animations for each action
    - Weapon swapping is added
    	-> Create weapon asset, add sprite, drag onto players 'weapon'
    	object (see hierarchy)
    - A bit of organizing, feel free to do more
    8f043592
    History
    New character art and animations
    Jordan authored
    - Character is now sperated into seperate parts
    - All new animations for each action
    - Weapon swapping is added
    	-> Create weapon asset, add sprite, drag onto players 'weapon'
    	object (see hierarchy)
    - A bit of organizing, feel free to do more
PlayerMovement.cs 3.22 KiB
using UnityEngine;
using System.Collections;

public class PlayerMovement : MonoBehaviour
{

    private Rigidbody2D Player;
    private Animator myAnimator;

    [SerializeField]
    private float speed;
    private bool LookRight = true;
    private bool canMove = true;
    private float attackSpeedCooldown;

    // Use this for initialization
    void Start()
    {
        //weapons[currentWeapon].attackSpeed = 1f;
        Player = GetComponent<Rigidbody2D>();
        myAnimator = GetComponent<Animator>(); //required for animation *******
    }

    // Update is called once per frame
    void Update()
    {
        float h = Input.GetAxis("Horizontal");
        float i = Input.GetAxis("Vertical");

        Movement(h, i);
        FlipPlayer(h);
        RotatePlayerZaxis(h);
        attack();
        block();
    }

    //controlling the characters movement 
    private void Movement(float horizontal, float vertical)
    {

        if (canMove)
        {
            Player.velocity = new Vector2(speed * horizontal, speed * vertical);

            myAnimator.SetFloat("speed", (Mathf.Abs(horizontal) + Mathf.Abs(vertical))); //required for animation *******
            }
        }

    
    private void attack() {
        attackSpeedCooldown -= Time.deltaTime;
        if (Input.GetKeyDown(KeyCode.Space) && attackSpeedCooldown <= 0) {
            //attackSpeedCooldown = weapons[currentWeapon].attackSpeed;
			attackSpeedCooldown = 1;
            myAnimator.SetBool("attack", true);
        }
        else {
            myAnimator.SetBool("attack", false);
        }
    }

    private void block()
    {
        if (Input.GetKeyDown(KeyCode.E))
        {
            myAnimator.SetBool("block", true);
            Player.velocity = Vector2.zero;
            canMove = false;

        }
        if (Input.GetKeyUp(KeyCode.E))
        {
            myAnimator.SetBool("block", false);
            canMove = true;

        }

    }



    //faces the characters body in the appropriate direction 
    private void FlipPlayer(float horizontal)
    {
        if (horizontal > 0 && !LookRight || horizontal < 0 && LookRight)
        {
            LookRight = !LookRight;

            Vector3 theScale = transform.localScale;
            theScale.x *= -1;
            transform.localScale = theScale;
        }
    }
 

    //keeps the character from falling over
    private void RotatePlayerZaxis(float horizonal)
    {
        Vector3 myRotation = gameObject.transform.rotation.eulerAngles;
        Vector3 Pos = transform.localPosition;
        //if the player does not need to be climbing up an angle, the character will be fixed upright
        if (Pos.x < -4f || Pos.x > 5f)
        {
            transform.localRotation = Quaternion.Euler(0, 0, 0);
        }


        else
        {

            //allows the character to rotate but not past 90 degrees either direction when climbing 
            if (myRotation.z > 90)
            {
                myRotation.z = Mathf.Clamp(0, 0, 0);
                transform.rotation = Quaternion.Euler(myRotation);
            }

            if (myRotation.z < -90)
            {
                myRotation.z = Mathf.Clamp(0, 0, 0);
                transform.rotation = Quaternion.Euler(myRotation);
            }
        }
    }
}