Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class PlayerMovement : MonoBehaviour
- {
- public float m_RunSpeed = 40.0f;
- public CharacterController2D m_CharacterController = null;
- private Animator m_Animator = null;
- float m_HorizontalInput = 0.0f;
- bool m_WantsToJump = false;
- bool m_IsInAttack = false;
- bool m_WasFacingRight = false;
- void Start()
- {
- m_Animator = GetComponent<Animator>();
- m_WasFacingRight = m_CharacterController.IsFacingRight();
- }
- void Update ()
- {
- m_HorizontalInput = 0.0f;
- if(!m_IsInAttack)
- {
- m_HorizontalInput = Input.GetAxisRaw("Horizontal");
- m_Animator.SetBool("IsRunning", Mathf.Abs(m_HorizontalInput) > 0.0f);
- m_Animator.SetFloat("VerticalSpeed", m_CharacterController.GetVerticalSpeed());
- m_WantsToJump = Input.GetButtonDown("Jump");
- if(m_CharacterController.IsGrounded())
- {
- if(Input.GetButtonDown("Fire1"))
- {
- m_Animator.SetTrigger("Attack");
- }
- if(m_WasFacingRight != m_CharacterController.IsFacingRight())
- {
- m_Animator.SetTrigger("ChangeDirection");
- m_WasFacingRight = m_CharacterController.IsFacingRight();
- }
- }
- }
- }
- void FixedUpdate()
- {
- bool didJump = m_CharacterController.Move(m_HorizontalInput * m_RunSpeed * Time.fixedDeltaTime, m_WantsToJump);
- if(didJump)
- {
- m_Animator.SetTrigger("Jump");
- }
- m_WantsToJump = false;
- }
- void StartAttack()
- {
- m_Animator.SetBool("IsRunning", false);
- m_IsInAttack = true;
- m_CharacterController.CanMove = false;
- }
- void EndAttack()
- {
- m_IsInAttack = false;
- m_CharacterController.CanMove = true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement