Orienting 2D sprite

Godot Version

4.3stable.mono.official

Question

Hi,
I’m working on a top-down space shooter with a rocket (imagine a triangle). I have problems with sprite, maybe scene, orientation.
The sprite (rocket) is supposed to be pointing up when idle or on start, but otherwise be oriented towards the mouse pointer. But when I use LookAt() with what is returned from GetViewport().GetMousePosition() the sprite is rotated for about 90 degrees less that what I expect it to be. For example if cursor is above the sprite, rocket points to the left (9 o’clock), the rocket points up when cursor is at its right side.

private const float NinetyDegrees = Mathf.Pi / 2;

if (InputType == ControlScheme.Keyboard)
{
	movementDirection = GetKeyboardMovement();

	var mousePosition = GetViewport().GetMousePosition();
	LookAt(mousePosition);
	// Rotation += NinetyDegrees;
}

The Rotation += NinetyDegrees kinda solves the problem because sprite is then correctly oriented relative to mouse cursor, but it then also idles pointing to the right (3 o’clock).

Any help is appreciated.

That’s how look_at() method works - it rotates your node so that its local x+ axis is rotated towards the given position.

You can rotate your whole sprite to be facing to the x+ as forward.

2 Likes

Try using

get_global_mouse_position()

@wchc you are correct, so I accepted your solution. I guess the right thing was to read the documentation :sweat_smile:

For posterity, this is how I “fixed” the issue for this crawl-forward game, that is vertically oriented:

public const float NinetyDegrees = Mathf.Pi / 2;

public override void _Process(double delta)
{
  Vector2 movementDirection = Vector2.Zero;

  if (InputType == ControlScheme.Keyboard)
  {
    movementDirection = GetKeyboardMovement();

    var mousePosition = GetViewport().GetMousePosition();
    LookAt(mousePosition);
    Rotation += NinetyDegrees;
  }
  else if (InputType == ControlScheme.Gamepad)
  {
    movementDirection = GetGamepadMovement();

    Vector2 aimVector = Input.GetVector("aim_left", "aim_right", "aim_up", "aim_down");
    if (aimVector.Length() != 0)
    {
      Rotation = aimVector.Angle() + NinetyDegrees;
    }
    else
    {
      // resets scene to looking up if not aiming with right gamepad stick
      Rotation = 0;
    }
  }

  Position += movementDirection * (float)delta;
  Position = new Vector2(
      x: Mathf.Clamp(Position.X, 0, ScreenSize.X),
      y: Mathf.Clamp(Position.Y, 0, ScreenSize.Y)
  );
}

It’s not the cleanest one, but it does what need it to do.

Thanks to everyone for helping me.

1 Like