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 CharacterController controller;
- public Animator animator;
- public float runSpeed = 40f;
- public bool isPlayer2 = false;
- float horizontalMove = 0f;
- bool jump = false;
- bool crouch = false;
- // Update is called once per frame
- void Update()
- {
- if (!isPlayer2)
- horizontalMove = Input.GetAxisRaw("Horizontal") * runSpeed;
- else if (isPlayer2)
- horizontalMove = Input.GetAxisRaw("Horizontal2") * runSpeed;
- animator.SetFloat("Speed", Mathf.Abs(horizontalMove));
- if (!isPlayer2 && Input.GetButtonDown("Jump"))
- {
- jump = true;
- animator.SetBool("IsJumping", true);
- }
- else if (isPlayer2 && Input.GetButtonDown("Jump2"))
- {
- jump = true;
- animator.SetBool("IsJumping", true);
- }
- if (!isPlayer2 && Input.GetButton("Crouch"))
- {
- crouch = true;
- }
- else if (!isPlayer2 && Input.GetButtonUp("Crouch"))
- {
- crouch = false;
- }
- else if (isPlayer2 && Input.GetButton("Crouch2"))
- {
- crouch = true;
- } else if (isPlayer2 && Input.GetButtonUp("Crouch2"))
- {
- crouch = false;
- }
- }
- void FixedUpdate() // better method for movement
- {
- controller.Move(horizontalMove * Time.fixedDeltaTime, crouch, jump);
- jump = false;
- }
- public void OnLanding()
- {
- animator.SetBool("IsJumping", false);
- }
- public void OnCrouching(bool isCrouching)
- {
- animator.SetBool("IsCrouching", isCrouching);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement