2d platformer dash not working godot c#

Godot Version

godot 4.2.1 stable-mono win64

Question

I have been working on a small action platformer in godot 4 with c#, and so far all I have put together a very basic platformer controller, and I was wondering how I might go about adding the ability to dash to my script, as whatever I am trying now does not seem to work. Here is the script in question:

 `using Godot;
using System;
public partial class Player : CharacterBody2D
{

	public bool CanDash = true;
	public int dashes = 1;
	public float movementSpeed = 150f;
	public float jumpVelocity = 200f;
	public float CoyoteTime = 0.4f;
	public float CoyoteCount;
	public float gravity = ProjectSettings.GetSetting("physics/2d/default_gravity").AsSingle();
	// Called when the node enters the scene tree for the first time.
	public override void _Ready()
	{
	}

	// Called every frame. 'delta' is the elapsed time since the previous frame.
	public override void _PhysicsProcess(double delta)
	{
		Vector2 velocity = Velocity;
		if(IsOnFloor() == false)
		{
			velocity.Y +=  gravity * (float)delta;
		}
		if(IsOnFloor())
		{
			CoyoteCount = CoyoteTime;
			dashes = 1;
		}else{
			CoyoteCount -= (float)delta;
		}

		velocity.X = 0;
		if(Input.IsActionPressed("Left"))
		{
			velocity.X = Mathf.Lerp(velocity.X, -movementSpeed, 0.8f);
		}else if(Input.IsActionPressed("Right"))
		{
			velocity.X = Mathf.Lerp(velocity.X, movementSpeed, 0.8f);
		}

		if(CoyoteCount > 0 && Input.IsActionJustPressed("Jump"))
		{
			velocity.Y = -jumpVelocity;
			CoyoteCount = 0;
		}
		
		if(Input.IsActionJustReleased("Jump") && velocity.Y < 0)
		{
			velocity.Y = velocity.Y * 0.1f;
		}

		if(CanDash)
		{
			if(Input.IsActionJustPressed("Jump"))
			{
				if(!IsOnFloor())
				{
					if(dashes > 0)
					{
						velocity.X += movementSpeed*10;
						dashes -= 1;
					}
				}
			}
		}

		Velocity = velocity;
		MoveAndSlide();
	}
}`

my dash currntly just adds an arbitrary value to the velocity, but for some reason this has little to no effect on the characters actual movement, besides just snapping them forward a couple pixels. Any help would be greatly apprecieated.

The only part of my code affected by delta is gravity? I don’t think it has any effect on the dash.

I suspect it’s because you’re setting Velocity.X to 0.

When you dash, it sets Velocity.X to 10 times the movement speed, then you MoveAndSlide exactly once. The very next time _PhysicsProcess gets called, you reset Velocity.X back to 0, and the dash is effectively cancelled.

The way you’re setting up Velocity.X up above is also a bit odd. You’re using Mathf.Lerp to adjust the Velocity.X 80% of the way to the movement speed, but because you reset Velocity.X every time the physics get processed, you never actually reach the full movement speed. You’re also never actually accelerating, which is probably what you meant to do with Mathf.Lerp.

I would suggest changing that to something a bit more like this:

// Make sure to only update the movement speed when not midair dashing.
if (!isCurrentlyDashing) {
    float desiredMovement = Input.GetAxis("Left", "Right") * movementSpeed;
    float newHorizontalMovement = Mathf.MoveToward(Velocity.X, desiredMovement, someAccelerationValue * delta);
    Velocity = Velocity with { X = newHorizontalMovement };
}

Eventually, you’ll probably want to set up a basic state machine to control when you can or can’t dash

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.