Advertisement
evelynshilosky

PlayerMovement - Part 33

Mar 7th, 2024
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.45 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.  
  9.     public float speed = 12f;
  10.     public float gravity = -9.81f * 2;
  11.     public float jumpHeight = 3f;
  12.  
  13.     public Transform groundCheck;
  14.     public float groundDistance = 0.4f;
  15.     public LayerMask groundMask;
  16.  
  17.     Vector3 velocity;
  18.  
  19.     bool isGrounded;
  20.  
  21.     private Vector3 lastPosition = new Vector3(0f, 0f, 0f);
  22.     public bool isMoving;
  23.  
  24.     // Update is called once per frame
  25.     void Update()
  26.     {
  27.         if (DialogueSystem.Instance.dialogueUIActive == false && StorageManager.Instance.storageUIOpen == false)
  28.         {
  29.             Movement();
  30.         }
  31.     }
  32.  
  33.  
  34. public void Movement()
  35.     {
  36.         if (!InventorySystem.Instance.isOpen && !CraftingSystem.Instance.isOpen && !MenuManager.Instance.isMenuOpen && !QuestManager.Instance.isQuestMenuOpen) //To Lock PlayerMovement when screen is open
  37.         {
  38.             //checking if we hit the ground to reset our falling velocity, otherwise we will fall faster the next time
  39.             isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
  40.  
  41.             if (isGrounded && velocity.y < 0)
  42.             {
  43.                 velocity.y = -2f;
  44.             }
  45.  
  46.             float x = Input.GetAxis("Horizontal");
  47.             float z = Input.GetAxis("Vertical");
  48.  
  49.             //right is the red Axis, foward is the blue axis
  50.             Vector3 move = transform.right * x + transform.forward * z;
  51.  
  52.             controller.Move(move * speed * Time.deltaTime);
  53.  
  54.             //check if the player is on the ground so he can jump
  55.             if (Input.GetButtonDown("Jump") && isGrounded)
  56.             {
  57.                 //the equation for jumping
  58.                 velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
  59.             }
  60.  
  61.             velocity.y += gravity * Time.deltaTime;
  62.  
  63.             controller.Move(velocity * Time.deltaTime);
  64.  
  65.             if (lastPosition != gameObject.transform.position && isGrounded == true)
  66.             {
  67.                 isMoving = true;
  68.                 SoundManager.Instance.PlaySound(SoundManager.Instance.grassWalkSound);
  69.             }
  70.             else
  71.             {
  72.                 isMoving = false;
  73.                 SoundManager.Instance.grassWalkSound.Stop();
  74.             }
  75.             lastPosition = gameObject.transform.position;
  76.         }
  77.     }
  78. }
  79.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement