|Attention||Topic was automatically imported from the old Question2Answer platform.|
I’m trying to create a character with completely physics-based movement. It’s supposed to be able to move along the X and Y axes, as well as rotate. The movement works fine, but I can’t get the thing to rotate. Can anyone help me?
I tried to use the code found at
However, even when copy-pasting the code, the movement works fine, but the thing still won’t rotate. No error, it just won’t do anything when the keys are pressed.
“Can sleep” is turned off.
This is my version of the code (slightly modified but the rotation part is the same):
extends RigidBody2D var max_speed = 250.0 var thrust = 250 var torque = 20000 func _integrate_forces(state): if state.linear_velocity.length()>max_speed: state.linear_velocity = state.linear_velocity.normalized()*max_speed if Input.is_action_pressed("move_forward"): set_applied_force(Vector2(0, -thrust).rotated(rotation)) elif Input.is_action_pressed("move_backward"): set_applied_force(Vector2(0, thrust).rotated(rotation)) elif Input.is_action_pressed("strafe_left"): set_applied_force(Vector2(-thrust, 0).rotated(rotation)) elif Input.is_action_pressed("strafe_right"): set_applied_force(Vector2(thrust, 0).rotated(rotation)) else: set_applied_force(Vector2()) var rotation_dir = 0 if Input.is_action_pressed("rotate_right"): rotation_dir += 1 if Input.is_action_pressed("rotate_left"): rotation_dir -= 1 set_applied_torque(rotation_dir * torque)
Not to be patronising, but did you make sure to bind the input actions in the project settings?
CardboardComputers | 2021-08-30 02:53
Not patronizing, I wondered about that, too. I double-checked the input, it works fine.
I found some articles that said a collision shape or collision polygon is required for set_applied_torque to function. I didn’t have that. Added one in now. Still doesn’t work.
Edit: I’m using set_angular_velocity now. That works. Multiple articles state one should use set_applied_torque, but if that doesn’t work, what can I do.
I’d still like to know why set_applied_torque doesn’t work, especially since that’s the one all the rigidbody tutorials are using, so if anyone knows, please, tell me.
Irolan | 2021-08-30 19:48