Added Sprite Swap and Horizontal flipping to character controller
This commit is contained in:
@@ -6,23 +6,76 @@ public partial class Player3D : CharacterBody3D
|
||||
{
|
||||
[Export] private float _speed = 1.0f;
|
||||
[Export] private Camera3D _camera;
|
||||
[Export] private Sprite3D _frontSprite;
|
||||
[Export] private Sprite3D _sideSprite;
|
||||
|
||||
private bool _sideFlipped;
|
||||
|
||||
public override void _PhysicsProcess(double delta)
|
||||
{
|
||||
if (!IsOnFloor()) Velocity += Vector3.Down * (float) delta;
|
||||
|
||||
|
||||
var inputDir = Input.GetVector("move_left", "move_right", "move_up", "move_down");
|
||||
if (inputDir == Vector2.Zero)
|
||||
return;
|
||||
|
||||
MoveOnInput(inputDir, delta);
|
||||
SwitchSprites(inputDir);
|
||||
}
|
||||
|
||||
private void MoveOnInput(Vector2 inputDir, double delta)
|
||||
{
|
||||
inputDir = inputDir.Rotated(-_camera.GlobalRotation.Y);
|
||||
var direction = (Transform.Basis * new Vector3(inputDir.X, 0, inputDir.Y)).Normalized();
|
||||
if (direction != Vector3.Zero)
|
||||
{
|
||||
Velocity = new Vector3(direction.X * _speed, Velocity.Y, direction.Z * _speed * (float) delta * Scale.X);
|
||||
}
|
||||
Velocity = new Vector3(direction.X * _speed * (float) delta * Scale.X, Velocity.Y, direction.Z * _speed * (float) delta * Scale.Z);
|
||||
else
|
||||
{
|
||||
Velocity = Velocity.MoveToward(new Vector3(0, 0, 0), _speed);
|
||||
}
|
||||
|
||||
MoveAndSlide();
|
||||
}
|
||||
|
||||
private void SwitchSprites(Vector2 inputDir)
|
||||
{
|
||||
float X = inputDir.X;
|
||||
float Y = inputDir.Y;
|
||||
|
||||
if (X == 0.0 && Y == 0.0)
|
||||
{
|
||||
ActivateFrontSprite(true);
|
||||
return;
|
||||
}
|
||||
|
||||
if (X != 0)
|
||||
{
|
||||
ActivateFrontSprite(false);
|
||||
|
||||
if (X > 0.0f)
|
||||
{
|
||||
_sideFlipped = false;
|
||||
_sideSprite.FlipH = _sideFlipped;
|
||||
}
|
||||
|
||||
if (X < 0.0f)
|
||||
{
|
||||
_sideFlipped = true;
|
||||
_sideSprite.FlipH = _sideFlipped;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (Y != 0)
|
||||
{
|
||||
ActivateFrontSprite(true);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void ActivateFrontSprite(bool activate)
|
||||
{
|
||||
_frontSprite.Visible = activate;
|
||||
_sideSprite.Visible = !activate;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user