Anonymous Anonymous - 17 days ago
99 0

No description

C#

Player Movement

using UnityEngine;
using System.Collections;

public class Player2 : MonoBehaviour {

    public float speed = 5f;
    public float jumpSpeed = 8f;
    private float HorizontalMove = 0f;
    private float VerticalMove = 0f;
    private Rigidbody2D rigidBody;
    public Transform groundCheckPoint;
    public float groundCheckRadius;
    public LayerMask groundLayer;
    private bool isTouchingGround;
    private Animator playerAnimation;
    private BoxCollider2D boxCollider;
    float crouchHeight = 1f;
    float crouchOffset = -0.88f;

    // Use this for initialization
    void Start () {
        rigidBody = GetComponent<Rigidbody2D>();
        playerAnimation = GetComponent<Animator>();
        boxCollider = GetComponent<BoxCollider2D>();
    }
	
	// Update is called once per frame
    void Update ()
    {
        isTouchingGround = Physics2D.OverlapCircle(groundCheckPoint.position, groundCheckRadius, groundLayer);
        HorizontalMove = Input.GetAxis("Horizontal");
        VerticalMove = Input.GetAxis("Vertical");

        if (HorizontalMove > 0f)
        {
            rigidBody.velocity = new Vector2(HorizontalMove * speed, rigidBody.velocity.y);
            transform.localScale = new Vector2(1f, 1f);
            playerAnimation.Play("warriors_walk");
        }
        else if (HorizontalMove < 0f)
        {
            rigidBody.velocity = new Vector2(HorizontalMove * speed, rigidBody.velocity.y);
            transform.localScale = new Vector2(-1f, 1f);
            playerAnimation.Play("warriors_walk");
        }
        else
        {
            rigidBody.velocity = new Vector2(0, rigidBody.velocity.y);
            playerAnimation.Play("warriors_idle");
        }

        if (Input.GetButtonDown("Jump") && isTouchingGround)
        {
            rigidBody.velocity = new Vector2(rigidBody.velocity.x, jumpSpeed);
            playerAnimation.Play("warriors_jump");
        }

        if(Input.GetButton("Crouch"))
        {
            boxCollider.size = new Vector2(boxCollider.size.x, crouchHeight);
            boxCollider.offset = new Vector2(0, crouchOffset);
            playerAnimation.Play("warriors_crouch");
        }
    }
}