I have a problem with player rotating to the mouse position. I'm using the code below (line 21 is the only code for rotating the player). The problem is in the attached video.
using Godot;
using System;
public partial class Player : CharacterBody2D
{
[Export] public int speed { get; set; } = 100;
private Vector2 screen_size;
private float start_rotation = 0f;
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
screen_size = GetViewportRect().Size;
}
// Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _Process(double delta)
{
HandlePlayerMovement(delta);
LookAt(GetGlobalMousePosition()); // Code for looking at the mouse cursor
}
public void HandlePlayerMovement(double delta)
{
var velocity = Vector2.Zero;
if (Input.IsActionPressed("move_up"))
{
velocity.Y -= 1;
}
if (Input.IsActionPressed("move_down"))
{
velocity.Y += 1;
}
if (Input.IsActionPressed("move_left"))
{
velocity.X -= 1;
}
if (Input.IsActionPressed("move_right"))
{
velocity.X += 1;
}
// if(Input.IsActionPressed("move_boost"))
// {
// }
// if (Input.IsActionJustPressed("move_phase"))
// {
// }
if (velocity.Length() > 0)
{
velocity = velocity.Normalized() * speed;
}
Position += velocity * (float)delta;
Position = new Vector2(
x: Mathf.Clamp(Position.X, 0, screen_size.X),
y: Mathf.Clamp(Position.Y, 0, screen_size.Y)
);
}
}
Can anyone help with this?