FreddieLumsden
FreddieLumsden

Reputation: 69

Godot: How can I rotate player in direction of travel?

Hi there I am trying to rotate my player into the direction that it is travelling in godot. Here is the code:

if Input.is_action_pressed("move_forward"):
    rotate_object_local(Vector3.UP, 1)
    transform.basis = Basis()
if Input.is_action_pressed("move_backward"):
    rotate_object_local(Vector3.UP, -1)
    transform.basis = Basis()
if Input.is_action_pressed("move_left"):
    rotate_object_local(Vector3.LEFT, 1)
    transform.basis = Basis()
if Input.is_action_pressed("move_right"):
    rotate_object_local(Vector3.LEFT, -1)
    transform.basis = Basis()

I've gone through countless forums and tutorials trying to figure out why it isn't working. Any help is much appreciated! :)

Upvotes: 1

Views: 2974

Answers (1)

Theraot
Theraot

Reputation: 40170

A Transform in Godot has two parts:

  • basis: a Basis that holds the direction and scale of each axis. Essentially a 3 by 3 matrix.
  • origin: a Vector3 that holds the translation.

Now, look at your code. You are setting the basis to the default:

transform.basis = Basis()

Here Basis() is a call to the default constructor of Basis, which gives you an identity Basis.

And, as you would expect, that will reset any rotation. It is undoing whatever rotate_object_local did. Thus the posted code has no effect.

If don't set the basis, you should be able to observe rotation.


Presumably you wanted to reset the rotation before applying the new one. For example:

    transform.basis = Basis()
    rotate_object_local(Vector3.UP, 1)

Upvotes: 1

Related Questions