I'm currently making spaceship controller script. Control is simple, W/S to move forward/backward, A/D/ to rotate ship by Y Axis(yaw).
Spaceship is RigidBody node and has CollisionShape as children.
Here's the code for move forward/backward:
export var speed = 1
export var max_speed = 10
var current_speed = 0
export var key_acceleration = KEY_W
export var key_deceleration = KEY_S
func process_movement(delta):
if Input.is_key_pressed(key_acceleration):
current_speed -= speed * delta
elif Input.is_key_pressed(key_deceleration):
current_speed += speed * delta
current_speed = clamp(current_speed, -max_speed, max_speed)
linear_velocity = Vector3(0, 0, current_speed)
I need to limit it's maximum speed, so I updated velocity directly, not using something like add_force.
Next is rotating code:
export var yawing_speed = 0.01
export var max_yawing_speed = 0.02
export var yaw_stop_threshold = 0.001
var current_yaw = 0
func process_yaw(delta):
if Input.is_key_pressed(key_yaw_left):
current_yaw += yawing_speed * delta
elif Input.is_key_pressed(key_yaw_right):
current_yaw += -yawing_speed * delta
else:
if current_yaw > 0:
current_yaw -= yawing_speed * delta
if current_yaw < yaw_stop_threshold:
current_yaw = 0
elif current_yaw < 0:
current_yaw += yawing_speed * delta
if current_yaw > yaw_stop_threshold:
current_yaw = 0
current_yaw = clamp(current_yaw, -max_yawing_speed, max_yawing_speed)
rotation.y += current_yaw
In this code, I updated rotation.y directly. Each of this function works if run separately, however run these together, it only changed rotation, not velocity.
func _physics_process(delta):
process_movement(delta)
process_yaw(delta)
Change order of process_movement and process_yaw doesn't change anything. If I just run one of process_movement and process_yaw, it works perfectly.
Why my rigidbody ignored velocity and only rotated? Any advice will very appreciate it.