player_control for godot

mail@pastecode.io avatar
unknown
csharp
9 days ago
5.5 kB
4
Indexable
Never
sing Godot;
using System;

public partial class Player_Control : CharacterBody3D
{
	[ExportGroup("DEBUG")]
	[Export] private Label speedLabel;
	[Export] private Label targetSpeedLabel;

	private Vector2 mouseMotion = Vector2.Zero;

	[ExportGroup("MouseLook")]
	[Export] private Node3D CameraPivot;
	[Export] private float mouseSensitivity;

	[ExportGroup("Abilities")]
	[Export] private float walkingSpeed = 5.0f ;
	[Export] private float runningSpeed = 8.0f;
	[Export] private float jumpForce = 4.5f;

	private float currentSpeed;
	private float targetSpeed;

	[ExportSubgroup("Crouching")]
	[Export] private float crouchingSpeed = 3.4f;
	[Export(PropertyHint.Range, "0.2, 1.5, 1.0")] private float crouchHeight { get; set;}

	private float originalHeight;
	private float currentHeight;
	private float targetHeight;

	private float crouchLerp;

	//Bool States
	private bool isCrouching = false;

	// Get the gravity from the project settings to be synced with RigidBody nodes.
	public float gravity = ProjectSettings.GetSetting("physics/3d/default_gravity").AsSingle();
	private float deltaTime;
	private Vector2 inputDir;

	private CollisionShape3D collisionShape;
	private CapsuleShape3D CS;

    public override void _Ready()
    {
		collisionShape = GetNode<CollisionShape3D>("CollisionShape3D");
		CS = new CapsuleShape3D();
		targetHeight = crouchHeight;

		//Set originalHeight
		if(collisionShape != null)  
		{ 
			if(collisionShape.Shape is CapsuleShape3D capsuleShape)
			{
				originalHeight = capsuleShape.Height;
				//Debug.Print(originalHeight.ToString());
			}
		}
		

        //base._Ready();

		Input.MouseMode = Input.MouseModeEnum.Captured;
    }

    public override void _PhysicsProcess(double delta)
	{
		deltaTime = (float)delta;

		_CameraRotation();
		Vector3 velocity = Velocity;

		// Add the gravity.
		if (!IsOnFloor())
			velocity.Y -= gravity * (float)delta;

		// Handle Jump.
		if (Input.IsActionJustPressed("ui_accept") && IsOnFloor())
			velocity.Y = jumpForce;

		// Get the input direction and handle the movement/deceleration.
		// As good practice, you should replace UI actions with custom gameplay actions.
		inputDir = Input.GetVector("ui_left", "ui_right", "ui_up", "ui_down");
		Vector3 direction = (Transform.Basis * new Vector3(inputDir.X, 0, inputDir.Y)).Normalized();
		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();
		_AdjustSpeed();

		_Crouch();
	}

	public override void _Input(InputEvent @event)
	{

		if(@event is InputEventMouseMotion mouseEvent)
		{
			if(Input.MouseMode == Input.MouseModeEnum.Captured)
			{
				mouseMotion = -mouseEvent.Relative * 0.001f;
			}
		}

		if(Input.IsActionPressed("ui_cancel"))
		{
			Input.MouseMode = Input.MouseModeEnum.Visible;
		}

		if(Input.IsActionPressed("Ctrl"))
		{
		   isCrouching = !isCrouching;
		}
	}

	void _AdjustSpeed()
	{
		if(!isCrouching)
		{
					//changes speed only if we inputDirection is anything than zero, no insta speeds.
			if(inputDir.X != 0 || inputDir.Y != 0)
			{
				if(!Input.IsActionPressed("Shift") && targetSpeed != walkingSpeed) targetSpeed = walkingSpeed;

				if(Input.IsActionJustPressed("Shift") && targetSpeed != runningSpeed) targetSpeed = runningSpeed;
				if(Input.IsActionJustReleased("Shift") && targetSpeed != walkingSpeed) targetSpeed = walkingSpeed;
			}

			else { if(targetSpeed != 0) targetSpeed = 0; }
		}	

		else
		{
			if(targetSpeed != crouchingSpeed) targetSpeed = crouchingSpeed;
		}

	
		speedLabel.Text = currentSpeed.ToString();
		

		//else{if(targetSpeed != 0) targetSpeed = 0; }

		currentSpeed = Mathf.Lerp(currentSpeed, targetSpeed, 2.5f * deltaTime);
	} 

	void _CameraRotation()
	
	{
		CameraPivot.RotateX(mouseMotion.Y);
		CameraPivot.RotationDegrees = new Vector3((float)Math.Clamp(CameraPivot.RotationDegrees.X, -90.0, 90.0), CameraPivot.RotationDegrees.Y, CameraPivot.RotationDegrees.Z);
	
	  RotateY(mouseMotion.X);
	  mouseMotion = Vector2.Zero;
	}

	void _Crouch()
	{
		if(crouchLerp < 1)
        crouchLerp += 2.5f * deltaTime;

       crouchLerp = Mathf.Clamp(crouchLerp, 0, 1);

        if (!isCrouching && CS.Height != originalHeight)
        {
            CS.Height = Mathf.Lerp(CS.Height, originalHeight, crouchLerp);
			collisionShape.Position = Position.Slerp(new Vector3(collisionShape.Position.X, targetHeight, collisionShape.Position.Z), crouchLerp);
            //CS.center = Vector3.Lerp(cc.center, new Vector3(0, orginalHeight / 2, 0), crouchLerp);
            //mouseLook.mainCamera.transform.position = Vector3.Slerp(mouseLook.mainCamera.transform.position, transform.position + new Vector3(0, 1.5f, 0), 5f * Time.deltaTime);
        }

        if(isCrouching && CS.Height != targetHeight)
        {
            CS.Height = Mathf.Lerp(CS.Height, targetHeight, crouchLerp);
			collisionShape.Position = Position.Slerp(new Vector3(collisionShape.Position.X, originalHeight, collisionShape.Position.Z), crouchLerp);
            //cc.center = Vector3.Lerp(cc.center, new Vector3(0, wantedHeight / 2, 0), crouchLerp);
            //mouseLook.mainCamera.transform.position = Vector3.Slerp(mouseLook.mainCamera.transform.position, transform.position + new Vector3(0, 0.5f, 0), 5f * Time.deltaTime);
        }

		collisionShape.Shape = CS;
		targetSpeedLabel.Text = CS.Height.ToString();
	}
}
Leave a Comment