0 votes

Video preview of the bug I'm facing →

Project source code

I am prototyping some top-down movement and tried implementing movement similar to Battlerite. Player movment with w a s d and rotating the character with the mouse--very traditional top-down movement controls.

The problem I am facing is when I am moving my character around it is rotating the character, even when I am not moving the mouse at all.

You can see in the video preview I provided at the top of this post that the mouse position does not move at all. I did some troubleshooting and somehow calling look_at while moving the character. If I comment out look_at there is no more rotating.

So my real question is: Is there a way to stop character rotation while only moving in my implementation? I only want mouse movement to control how much the character rotates.

My look_at-related code:

var drop_plane = Plane(Vector3.UP, player_bottom.global_transform.origin.y)

var ray_length = 1000
var mouse_position = get_viewport().get_mouse_position()
var from = camera.project_ray_origin(mouse_position)
var to = from + camera.project_ray_normal(mouse_position) * ray_length

var distance = Vector3.ZERO
distance.y = player_bottom.transform.origin.distance_to(body.transform.origin)

var intersects_at = drop_plane.intersects_ray(from, to)
var cursor_pos = Vector3()
if intersects_at:
  cursor_pos = intersects_at + distance

body.look_at(cursor_pos, Vector3.UP)
body.rotation = body.rotation * Vector3.UP
Godot version v3.3.rc7.mono.official
in Engine by (19 points)

1 Answer

0 votes
Best answer

Changing the following line corrects the problem!

var to = from + camera.project_ray_normal(mouse_position) * ray_length

to this:

var to = camera.project_ray_normal(mouse_position) * ray_length
# I just got rid of "from +"
by (19 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.