Advertisement
Guest User

PlayerMovement Script

a guest
Jun 5th, 2019
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.79 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class PlayerMovement : MonoBehaviour
  6. {
  7.     public CharacterController controller;
  8.     public Animator animator;
  9.  
  10.     public float runSpeed = 40f;
  11.     public bool isPlayer2 = false;
  12.  
  13.     float horizontalMove = 0f;
  14.     bool jump = false;
  15.     bool crouch = false;
  16.  
  17.     // Update is called once per frame
  18.     void Update()
  19.     {
  20.         if (!isPlayer2)
  21.             horizontalMove = Input.GetAxisRaw("Horizontal") * runSpeed;
  22.         else if (isPlayer2)
  23.             horizontalMove = Input.GetAxisRaw("Horizontal2") * runSpeed;
  24.  
  25.         animator.SetFloat("Speed", Mathf.Abs(horizontalMove));
  26.  
  27.         if (!isPlayer2 && Input.GetButtonDown("Jump"))
  28.         {
  29.             jump = true;
  30.             animator.SetBool("IsJumping", true);
  31.         }
  32.         else if (isPlayer2 && Input.GetButtonDown("Jump2"))
  33.         {
  34.             jump = true;
  35.             animator.SetBool("IsJumping", true);
  36.         }
  37.  
  38.         if (!isPlayer2 && Input.GetButton("Crouch"))
  39.         {
  40.             crouch = true;
  41.         }
  42.         else if (!isPlayer2 && Input.GetButtonUp("Crouch"))
  43.         {
  44.             crouch = false;
  45.         }
  46.         else if (isPlayer2 && Input.GetButton("Crouch2"))
  47.         {
  48.             crouch = true;
  49.         } else if (isPlayer2 && Input.GetButtonUp("Crouch2"))
  50.         {
  51.             crouch = false;
  52.         }
  53.     }
  54.  
  55.     void FixedUpdate() // better method for movement
  56.     {
  57.         controller.Move(horizontalMove * Time.fixedDeltaTime, crouch, jump);
  58.         jump = false;
  59.     }
  60.  
  61.     public void OnLanding()
  62.     {
  63.         animator.SetBool("IsJumping", false);
  64.     }
  65.  
  66.     public void OnCrouching(bool isCrouching)
  67.     {
  68.         animator.SetBool("IsCrouching", isCrouching);
  69.     }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement