Godot Version
4.1
Question
Hi, so I’m quite new to Godot I came from unity a bit ago and I’m trying to make a 2D character controller in c# but when i jump the character jumps twice no matter what the key for jump is
here is the code any help would be greatly appreciated, thanks in advance
using Godot;
using System;
using System.ComponentModel;
public partial class Player : CharacterBody2D
{
[ExportCategory("Attributes")]
[Export] private float movementSpeed = 150f;
[Export] private float acceleration = 7f;
[Export] private float deceleration = 10f;
[Export] private float jumpForce = -300f;
[ExportCategory("Gravity")]
[Export] private float gravity = 2000f;
[Export] private float terminalVelocity = 600f;
[ExportCategory("Inputs")]
[Export] private bool useRawInput = true;
private Vector2 input;
[Export] private StringName up = new StringName("Up"), left = new StringName("Left"), right = new StringName("Right"), down = new StringName("Down");
[Export] private Key jumpKey = Key.Space;
[Export] private bool didJump = false;
//velocity
private Vector2 velocity;
private float targetVelocity;
private float previousVelocityY, newVelocityY;
private Sprite2D sprite;
private AnimationPlayer anim;
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
sprite = GetNode<Sprite2D>("Sprite2D");
anim = GetNode<AnimationPlayer>("AnimationPlayer");
sprite.TopLevel = true;
}
// Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _Process(double delta)
{
GatherInput();
FlipSprite();
sprite.GlobalPosition = sprite.GlobalPosition.Lerp(GlobalPosition + Vector2.Up * 8 , (float)Engine.GetPhysicsInterpolationFraction());
}
public override void _PhysicsProcess(double delta)
{
MoveAndSlide();
CalculateVelocity((float)delta);
}
#region Input
//Gathers input from the player
private void GatherInput()
{
input = Input.GetVector(left, right, up, down);
if (useRawInput) input = input.GetRaw();
if (Input.IsKeyPressed(keycode:jumpKey))
{
didJump = true;
}
}
#endregion
#region Gravity
//Apply gravity to the charater body velocity
public void ApplyGravity(ref Vector2 velocity, float delta)
{
if (IsOnFloor()) return;
previousVelocityY = velocity.Y;
newVelocityY = velocity.Y + gravity * delta;
newVelocityY = Mathf.Clamp(newVelocityY, -Mathf.Inf, terminalVelocity);
velocity.Y = (previousVelocityY + newVelocityY) * 0.5f;
}
#endregion
#region Velocity
//calcualte the bi-directional velocity
private void CalculateVelocity(float delta)
{
velocity = Velocity;
CalculateVelocityY(ref velocity, delta);
CalculateVelocityX(ref velocity);
Velocity = velocity;
}
//calcualte the Y velocity
private void CalculateVelocityY(ref Vector2 vel, float delta)
{
HandleJump(ref vel);
ApplyGravity(ref vel, delta);
}
//calcualte the X velocity
private void CalculateVelocityX(ref Vector2 vel)
{
targetVelocity = input.X * movementSpeed;
vel.X = Mathf.MoveToward(vel.X, targetVelocity, Mathf.Sign(input.X) == Mathf.Sign(velocity.X)? acceleration: deceleration);
}
#endregion
#region Jump
private void HandleJump(ref Vector2 vel)
{
if(!IsOnFloor() || !didJump)
return;
didJump = false;
vel.Y = jumpForce;
GD.Print("Jump");
}
#endregion
#region Animation
//makes charater face the way of it's movement
private void FlipSprite()
{
sprite.FlipH = input.X switch { <0 => true, >0 => false, _ => sprite.FlipH};
}
private void AnimationStates()
{
//checks if charather is idle
if(Velocity.X == 0 && Velocity.Y == 0)
{
anim.Play("Idle");
}
}
#endregion
}