Advertisement
Guest User

Untitled

a guest
May 23rd, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.65 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class PlayerMovement : MonoBehaviour
  6. {
  7.  
  8.     public float m_RunSpeed = 40.0f;
  9.  
  10.     public CharacterController2D m_CharacterController = null;
  11.     private Animator m_Animator = null;
  12.  
  13.     float m_HorizontalInput = 0.0f;
  14.     bool m_WantsToJump = false;
  15.     bool m_IsInAttack = false;
  16.     bool m_WasFacingRight = false;
  17.  
  18.     void Start()
  19.     {
  20.         m_Animator = GetComponent<Animator>();
  21.         m_WasFacingRight = m_CharacterController.IsFacingRight();
  22.     }
  23.  
  24.     void Update ()
  25.     {
  26.         m_HorizontalInput = 0.0f;
  27.  
  28.         if(!m_IsInAttack)
  29.         {
  30.             m_HorizontalInput = Input.GetAxisRaw("Horizontal");
  31.             m_Animator.SetBool("IsRunning", Mathf.Abs(m_HorizontalInput) > 0.0f);
  32.             m_Animator.SetFloat("VerticalSpeed", m_CharacterController.GetVerticalSpeed());
  33.             m_WantsToJump = Input.GetButtonDown("Jump");
  34.  
  35.             if(m_CharacterController.IsGrounded())
  36.             {
  37.                 if(Input.GetButtonDown("Fire1"))
  38.                 {
  39.                     m_Animator.SetTrigger("Attack");
  40.                 }
  41.  
  42.                 if(m_WasFacingRight != m_CharacterController.IsFacingRight())
  43.                 {
  44.                     m_Animator.SetTrigger("ChangeDirection");
  45.                     m_WasFacingRight = m_CharacterController.IsFacingRight();
  46.                 }
  47.             }
  48.         }
  49.     }
  50.  
  51.     void FixedUpdate()
  52.     {
  53.         bool didJump = m_CharacterController.Move(m_HorizontalInput * m_RunSpeed * Time.fixedDeltaTime, m_WantsToJump);
  54.         if(didJump)
  55.         {
  56.             m_Animator.SetTrigger("Jump");
  57.         }
  58.  
  59.         m_WantsToJump = false;
  60.     }
  61.  
  62.     void StartAttack()
  63.     {
  64.         m_Animator.SetBool("IsRunning", false);
  65.         m_IsInAttack = true;
  66.         m_CharacterController.CanMove = false;
  67.     }
  68.  
  69.     void EndAttack()
  70.     {
  71.         m_IsInAttack = false;
  72.         m_CharacterController.CanMove = true;
  73.     }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement