Advertisement
Guest User

Player Movement Script

a guest
Sep 24th, 2023
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.54 KB | None | 0 0
  1. using Godot;
  2. using System;
  3. using System.Net.Http;
  4.  
  5. public partial class PlayerMovement : CharacterBody3D
  6. {
  7.     //Speed variables
  8.     private float currentSpeed;
  9.     [Export] private float walkSpeed = 5f, sprintSpeed = 8f, crouchSpeed = 3f, jumpVelocity = 4.5f;
  10.     float lerpSpeed = 10f;
  11.  
  12.     //General movement
  13.     [Export] private float mouseSense = 1f;
  14.     Vector3 direction;
  15.     Vector2 inputDir;
  16.     Vector3 velocity;
  17.     private float crouchingDepth = 0.5f;
  18.     float originalColliderHeight;
  19.     private float startingYHeadPos;
  20.  
  21.     [Export] private Node3D cameraHolder;
  22.     [Export] private CollisionShape3D standingCollider;
  23.     [Export] private CollisionShape3D crouchingCollider;
  24.     [Export] private float crouchLimit = 3f;
  25.  
  26.     //Bools
  27.     bool isCeilingUp = false;
  28.     bool isSprinting, isCrouching;
  29.  
  30.     // Get the gravity from the project settings to be synced with RigidBody nodes.
  31.     public float gravity = ProjectSettings.GetSetting("physics/3d/default_gravity").AsSingle();
  32.  
  33.     public override void _Ready()
  34.     {
  35.  
  36.         base._Ready();
  37.  
  38.         currentSpeed = walkSpeed;
  39.         startingYHeadPos = cameraHolder.Position.Y;
  40.  
  41.         Input.MouseMode = Input.MouseModeEnum.Captured;
  42.  
  43.  
  44.  
  45.     }
  46.  
  47.     public override void _Process(double delta)
  48.     {
  49.         base._Process(delta);
  50.  
  51.         HandleSprintingAndCrouching(delta);
  52.  
  53.  
  54.  
  55.     }
  56.  
  57.     public override void _Input(InputEvent @event)
  58.     {
  59.         base._Input(@event);
  60.         if (@event is InputEventMouseMotion ev)
  61.         {
  62.             HandleCameraRotation(ev);
  63.         }
  64.     }
  65.  
  66.     void HandleCameraRotation(InputEventMouseMotion ev)
  67.     {
  68.         RotateY(Mathf.DegToRad(-ev.Relative.X * mouseSense));
  69.         cameraHolder.RotateX(Mathf.DegToRad(-ev.Relative.Y * mouseSense));
  70.         float clampedXRotation = Mathf.Clamp(cameraHolder.Rotation.X, Mathf.DegToRad(-89f), Mathf.DegToRad(89f));
  71.         cameraHolder.Rotation = new Vector3(clampedXRotation, cameraHolder.Rotation.Y, cameraHolder.Rotation.Z);
  72.     }
  73.  
  74.     void HandleSprintingAndCrouching(double delta)
  75.     {
  76.  
  77.  
  78.         if (Input.IsActionPressed("sprint"))
  79.         {
  80.             isSprinting = true;
  81.             isCrouching = false;
  82.  
  83.  
  84.         }
  85.         else
  86.         {
  87.             isSprinting = false;
  88.         }
  89.  
  90.         if (Input.IsActionPressed("crouch"))
  91.         {
  92.             isCrouching = true;
  93.             isSprinting = false;
  94.             cameraHolder.Position = new Vector3(cameraHolder.Position.X, Mathf.Lerp(cameraHolder.Position.Y, startingYHeadPos - crouchingDepth, (float)delta * lerpSpeed), cameraHolder.Position.Z);
  95.  
  96.             crouchingCollider.Disabled = false;
  97.             standingCollider.Disabled = true;
  98.  
  99.         }
  100.         else if (!isCeilingUp)
  101.         {
  102.             isCrouching = false;
  103.             cameraHolder.Position = new Vector3(cameraHolder.Position.X, Mathf.Lerp(cameraHolder.Position.Y, startingYHeadPos, (float)delta * lerpSpeed), cameraHolder.Position.Z);
  104.  
  105.             crouchingCollider.Disabled = true;
  106.             standingCollider.Disabled = false;
  107.  
  108.         }
  109.         else if (isCeilingUp)
  110.         {
  111.             isSprinting = false;
  112.             isCrouching = true;
  113.         }
  114.  
  115.         #region Weirdconditionals man
  116.  
  117.  
  118.         if (isSprinting && isCrouching)
  119.         {
  120.             currentSpeed = crouchSpeed;
  121.  
  122.         }
  123.         else if (isSprinting)
  124.         {
  125.             currentSpeed = sprintSpeed;
  126.  
  127.         }
  128.         else if (isCrouching)
  129.         {
  130.             currentSpeed = crouchSpeed;
  131.         }
  132.         else
  133.         {
  134.             currentSpeed = walkSpeed;
  135.         }
  136.         #endregion
  137.  
  138.     }
  139.  
  140.     public override void _PhysicsProcess(double delta)
  141.     {
  142.         inputDir = Input.GetVector("left", "right", "forward", "backward");
  143.  
  144.         CheckForCeiling();
  145.  
  146.         velocity = Velocity;
  147.  
  148.         // Add the gravity.
  149.         if (!IsOnFloor())
  150.             velocity.Y -= gravity * (float)delta;
  151.  
  152.         // Handle Jump.
  153.         if (Input.IsActionJustPressed("jump") && IsOnFloor())
  154.             velocity.Y = jumpVelocity;
  155.  
  156.         // Get the input direction and handle the movement/deceleration.
  157.         // As good practice, you should replace UI actions with custom gameplay actions.
  158.  
  159.         Vector3 desiredDir = (Transform.Basis * new Vector3(inputDir.X, 0, inputDir.Y)).Normalized();
  160.         direction = direction.Lerp(desiredDir, (float)delta * lerpSpeed);
  161.  
  162.         if (direction != Vector3.Zero)
  163.         {
  164.             velocity.X = direction.X * currentSpeed;
  165.             velocity.Z = direction.Z * currentSpeed;
  166.         }
  167.         else
  168.         {
  169.             velocity.X = Mathf.MoveToward(Velocity.X, 0, currentSpeed);
  170.             velocity.Z = Mathf.MoveToward(Velocity.Z, 0, currentSpeed);
  171.         }
  172.  
  173.         Velocity = velocity;
  174.         MoveAndSlide();
  175.     }
  176.  
  177.     void CheckForCeiling()
  178.     {
  179.  
  180.         var spaceState = GetWorld3D().DirectSpaceState;
  181.         // use global coordinates, not local to node
  182.         var query = PhysicsRayQueryParameters3D.Create(Position, Position + Vector3.Up * crouchLimit);
  183.         query.Exclude = new Godot.Collections.Array<Rid> { GetRid() };
  184.         var result = spaceState.IntersectRay(query);
  185.  
  186.         if (result.Count > 0)
  187.         {
  188.             isCeilingUp = true;
  189.         }
  190.         else
  191.         {
  192.             isCeilingUp = false;
  193.  
  194.         }
  195.     }
  196.  
  197. }
  198.  
  199.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement