#Player look at mouse problem

51 messages · Page 1 of 1 (latest)

vast lodge
#

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?

#

Same issue with Rotate(GetAngleTo(GetGlobalMousePosition()));

fading bronze
#

Rotation = (GetGlobalMousePosition() - GlobalPosition of your node you want to rotate).Angle()

vast lodge
#

any Markers2D and such

fading bronze
#

its probably how your player is oriented in the scene file prehaps?

#

for example in my game the weapon rotates just fine using that code in the scene file the place where the points all line up is where it is going to rotate from

vast lodge
fading bronze
#

Oh wow! and it still doesnt work?

vast lodge
#

i found somewhere that the default rotation should be 90 degrees but it was the same issue even before

#

yes

fading bronze
#

thats interesting

#

no worries we'll figure it out

vast lodge
#

i also found out that the custom cursor is still there when i move out of the game window

#

but at top left the cursor disappears before i leave the window

#

so it seems like the game window is somewhat missplaced

#

seems like it is offset around 100 pixels X and Y

#

it's very weird

fading bronze
#

that im not sure about

vast lodge
#

i haven't found this bug anywhere before

#

but i don't know what i did wrong i only created the player scene with sprite collision polygon and camera2D and then i added the script and it isn't working

#

and in the settings i only changed the cursor and the stretch mode

fading bronze
#

did you alter the transform of the player at all in editor?

vast lodge
#

nope, everything set to 0

#

on all the nodes also

fading bronze
#

hmmm

vast lodge
#

i only changed the characterbody rotation to 90

#

but it worked the same before

#

i have it set to 0 in the video

#

i'll try to create a new project, just to see if it fixes it

fading bronze
#

Yeah maybe some weird setting was touched or something, I'm just doing a bit of research cause i have no idea why this wont work when it works just fine in my project with the same setup

#

trying to stay away from math cause the method should make things simple

vast lodge
#

yea i was also thinking about doing this with math until i found out about the lookat and rotate functions

#

so i tried it this way and this was the result

fading bronze
#

I just want to double check the characterbody is postioned here as well right? Im pretty sure it is cause you locked all your children together but just double check

#

because the way its rotating suggests that the origin is displaced

vast lodge
#

in the new project it works fine

#

i did all the same steps i did before

#

well i dont have the same sprite but i dont think that changes anything

#

i will try to give it the same sprite now

fading bronze
vast lodge
#

it's possible but i dont know how

fading bronze
#

im not entirely sure but i have to leave it here cause i have to go to work! sorry i wasnt much help and good luck! I'll be back around later

vast lodge
#

I think it's cause of the sprite, it's doing the same stuff again

fading bronze
#

rq it could be like some weird sizing of the sprite so the origin is there but the actual image origin isnt there

vast lodge
#

Thank you for the help tho

#

i will investigate further

#

not closing this thread tho, i might fix it still

#

it is the custom cursor

vast lodge