Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | __rm__dev |
I’m having trouble with the movement speed of my 2d platformer controller. The movement speed slows/gains speed on slopes. Here is a video showing the problem.
I haven’t really changed the default code that much, except using custom keys, adding gravity scale, and moving the input handling at process
Here is the code:
public const float Speed = 300.0f;
public const float JumpVelocity = -600.0f;
public float defaultGravity = ProjectSettings.GetSetting("physics/2d/default_gravity").AsSingle();
private float gravityScales = 1;
public float Gravity => gravityScales * defaultGravity;
private Vector2 prevPosition;
public override void _Process(double delta)
{
var velocity = Velocity;
var direction = Input.GetVector("move_left", "move_right", "ui_up", "ui_down");
if (direction != Vector2.Zero)
{
velocity.X = direction.X * Speed;
}
else
{
velocity.X = Mathf.MoveToward(Velocity.X, 0, Speed);
}
if (Input.IsActionJustPressed("ui_accept") && IsOnFloor())
velocity.Y = JumpVelocity;
Velocity = velocity;
}
public override void _PhysicsProcess(double delta)
{
var velocity = Velocity;
if (!IsOnFloor())
velocity.Y += Gravity * (float)delta;
FloorSnapLength = 32;
Velocity = velocity;
MoveAndSlide();
}