Guest User

Untitled

a guest
Oct 5th, 2025
18
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.86 KB | None | 0 0
  1. using System.IO.Compression;
  2. using NUnit.Framework;
  3. using UnityEngine;
  4.  
  5. public class PlayerMovement : MonoBehaviour
  6. {
  7.     private CharacterController ctrlr;
  8.     public float speed = 12f;
  9.     public float gravity = -9.81f * 2;
  10.     public float jumpHeight = 3f;
  11.     public float airjumps = 0f; // don't worry about this, I'll come back and deal with it in the future.
  12.  
  13.     public Transform groundCheck;
  14.     public float groundDistance = 0.4f;
  15.     public LayerMask groundMask;
  16.  
  17.     Vector3 velocity;
  18.     public bool isGrounded;
  19.     bool isMoving;
  20.     // Start is called once before the first execution of Update after the MonoBehaviour is created
  21.     void Start()
  22.     {
  23.         ctrlr = GetComponent<CharacterController>();
  24.     }
  25.  
  26.     // Update is called once per frame
  27.     void Update()
  28.     {
  29.         // check if we're touching the ground
  30.         isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
  31.         if (isGrounded && velocity.y < 0)
  32.         {
  33.             velocity.y = -2f;
  34.         }
  35.  
  36.         // map our inputs
  37.         float x = Input.GetAxis("Horizontal");
  38.         float z = Input.GetAxis("Vertical");
  39.  
  40.         // vector stuff
  41.         Vector3 movement = transform.right * x + transform.forward * z;
  42.  
  43.         // proper movement
  44.         ctrlr.Move(movement * speed * Time.deltaTime);
  45.         if (Input.GetButtonDown("Jump") && (isGrounded || airjumps > 0))
  46.         {
  47.             velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
  48.         }
  49.         velocity.y += gravity * Time.deltaTime;
  50.  
  51.         ctrlr.Move(velocity * Time.deltaTime);
  52.  
  53.         Vector3 lastPosition = gameObject.transform.position;
  54.  
  55.         if (lastPosition != gameObject.transform.position && isGrounded == true)
  56.         {
  57.             isMoving = true;
  58.         }
  59.         else
  60.         {
  61.             isMoving = false;
  62.         }
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment