Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Godot;
- using System;
- using System.Net.Http;
- public partial class PlayerMovement : CharacterBody3D
- {
- //Speed variables
- private float currentSpeed;
- [Export] private float walkSpeed = 5f, sprintSpeed = 8f, crouchSpeed = 3f, jumpVelocity = 4.5f;
- float lerpSpeed = 10f;
- //General movement
- [Export] private float mouseSense = 1f;
- Vector3 direction;
- Vector2 inputDir;
- Vector3 velocity;
- private float crouchingDepth = 0.5f;
- float originalColliderHeight;
- private float startingYHeadPos;
- [Export] private Node3D cameraHolder;
- [Export] private CollisionShape3D standingCollider;
- [Export] private CollisionShape3D crouchingCollider;
- [Export] private float crouchLimit = 3f;
- //Bools
- bool isCeilingUp = false;
- bool isSprinting, isCrouching;
- // Get the gravity from the project settings to be synced with RigidBody nodes.
- public float gravity = ProjectSettings.GetSetting("physics/3d/default_gravity").AsSingle();
- public override void _Ready()
- {
- base._Ready();
- currentSpeed = walkSpeed;
- startingYHeadPos = cameraHolder.Position.Y;
- Input.MouseMode = Input.MouseModeEnum.Captured;
- }
- public override void _Process(double delta)
- {
- base._Process(delta);
- HandleSprintingAndCrouching(delta);
- }
- public override void _Input(InputEvent @event)
- {
- base._Input(@event);
- if (@event is InputEventMouseMotion ev)
- {
- HandleCameraRotation(ev);
- }
- }
- void HandleCameraRotation(InputEventMouseMotion ev)
- {
- RotateY(Mathf.DegToRad(-ev.Relative.X * mouseSense));
- cameraHolder.RotateX(Mathf.DegToRad(-ev.Relative.Y * mouseSense));
- float clampedXRotation = Mathf.Clamp(cameraHolder.Rotation.X, Mathf.DegToRad(-89f), Mathf.DegToRad(89f));
- cameraHolder.Rotation = new Vector3(clampedXRotation, cameraHolder.Rotation.Y, cameraHolder.Rotation.Z);
- }
- void HandleSprintingAndCrouching(double delta)
- {
- if (Input.IsActionPressed("sprint"))
- {
- isSprinting = true;
- isCrouching = false;
- }
- else
- {
- isSprinting = false;
- }
- if (Input.IsActionPressed("crouch"))
- {
- isCrouching = true;
- isSprinting = false;
- cameraHolder.Position = new Vector3(cameraHolder.Position.X, Mathf.Lerp(cameraHolder.Position.Y, startingYHeadPos - crouchingDepth, (float)delta * lerpSpeed), cameraHolder.Position.Z);
- crouchingCollider.Disabled = false;
- standingCollider.Disabled = true;
- }
- else if (!isCeilingUp)
- {
- isCrouching = false;
- cameraHolder.Position = new Vector3(cameraHolder.Position.X, Mathf.Lerp(cameraHolder.Position.Y, startingYHeadPos, (float)delta * lerpSpeed), cameraHolder.Position.Z);
- crouchingCollider.Disabled = true;
- standingCollider.Disabled = false;
- }
- else if (isCeilingUp)
- {
- isSprinting = false;
- isCrouching = true;
- }
- #region Weirdconditionals man
- if (isSprinting && isCrouching)
- {
- currentSpeed = crouchSpeed;
- }
- else if (isSprinting)
- {
- currentSpeed = sprintSpeed;
- }
- else if (isCrouching)
- {
- currentSpeed = crouchSpeed;
- }
- else
- {
- currentSpeed = walkSpeed;
- }
- #endregion
- }
- public override void _PhysicsProcess(double delta)
- {
- inputDir = Input.GetVector("left", "right", "forward", "backward");
- CheckForCeiling();
- velocity = Velocity;
- // Add the gravity.
- if (!IsOnFloor())
- velocity.Y -= gravity * (float)delta;
- // Handle Jump.
- if (Input.IsActionJustPressed("jump") && IsOnFloor())
- velocity.Y = jumpVelocity;
- // Get the input direction and handle the movement/deceleration.
- // As good practice, you should replace UI actions with custom gameplay actions.
- Vector3 desiredDir = (Transform.Basis * new Vector3(inputDir.X, 0, inputDir.Y)).Normalized();
- direction = direction.Lerp(desiredDir, (float)delta * lerpSpeed);
- if (direction != Vector3.Zero)
- {
- velocity.X = direction.X * currentSpeed;
- velocity.Z = direction.Z * currentSpeed;
- }
- else
- {
- velocity.X = Mathf.MoveToward(Velocity.X, 0, currentSpeed);
- velocity.Z = Mathf.MoveToward(Velocity.Z, 0, currentSpeed);
- }
- Velocity = velocity;
- MoveAndSlide();
- }
- void CheckForCeiling()
- {
- var spaceState = GetWorld3D().DirectSpaceState;
- // use global coordinates, not local to node
- var query = PhysicsRayQueryParameters3D.Create(Position, Position + Vector3.Up * crouchLimit);
- query.Exclude = new Godot.Collections.Array<Rid> { GetRid() };
- var result = spaceState.IntersectRay(query);
- if (result.Count > 0)
- {
- isCeilingUp = true;
- }
- else
- {
- isCeilingUp = false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement