moving sideways in overhead game (with player rotation)

Attention Topic was automatically imported from the old Question2Answer platform.

Hi,
I’m trying create overhead game with player looking at mouse point and I have problem with sideways moving.I cannot use ** velocity.x -= 1** because player is rotating position to mouse point… Currently i did forward and backward moving feature:

``````if Input.is_action_pressed('ui_down'):
velocity = (-mouse_pos + position).normalized() * 5000
if Input.is_action_pressed('ui_up'):
velocity = (mouse_pos - position).normalized() * 10000
``````

PS: I just wanna make game with movement like in CS 2d.

Use the node’s transform for this - it contains the node’s local x and y axes. `transform.x` is your forward direction (pointing at the mouse).

For example:

``````func _physics_process(delta):
look_at(get_global_mouse_position())
if Input.is_action_pressed('up'):
velocity += transform.x * speed
if Input.is_action_pressed('down'):
velocity -= transform.x * speed

velocity = move_and_slide(velocity)
``````

ok, but how to create sideways moving? E.g. when i click left button the character is moving left(including rotation).

GameTester2 | 2020-07-14 09:10

``````if Input.is_action_pressed("right"):
velocity += transform.y * speed
if Input.is_action_pressed("left"):
velocity -= transform.y * speed
``````

kidscancode | 2020-07-14 15:34

I did something like this, but it doesn’t work properly(Player is making circular motion instead of turning left / right).

``````
if Input.is_action_pressed('ui_left'):
velocity = (mouse_pos - position).rotated(-90).normalized() * speed
if Input.is_action_pressed("ui_right"):
velocity = (mouse_pos - position).rotated(90).normalized() * speed
``````

`rotated()` uses radians, not degrees.

If you want to use the direction to the mouse, you can use `direction_to()`, which gives you a normalized vector.

kidscancode | 2020-07-14 15:36