Godot Version
v.4.2.1.stable.mono.official [b09f793f5]
Question
I am a beginner to using Godot in 3D space and I am trying to get to know it by creating a simple space fighter sim. I started out having my player controlled as a CharacterBody3D but then converted it to a RigidBody3D in order to be able to apply forces.
Now during my trial and error on how to use that I stumbled upon strange position glitches which turned out to happen, even if I do not apply forces. After a random number of seconds the position of my RigidBody teleports to a different, yet repeatable position.
If I apply a force it changes depending on the value I set.
I have to admit that I really do not understand what I am doing wrong and I have trouble finding posts with similar issues.
Here is my node tree for my Player Object (which is to be a spacefighter)
Player [RigidBody3D]
-- Camera3D [Camera3D]
-- ShipA [Node3D imported from a Blenderfile]
-- CollisionShape3D [CollisionShape3D with a simple BoxCollider]
Here is the code which handles mouse-based steering and a simple force appliance for moving (which is currently not working, there is still some bug in force-apply - nothing happens when pushing the button, but that is another topic)
extends RigidBody3D
var direction
var scaled_mouse_pos_x
var scaled_mouse_pos_y
var mouse_range_min = 0.01
var mouse_range_max = 0.9
var rot_vel_x
var rot_vel_y
func _process(delta):
# Throttle
direction = Input.get_axis("ui_up", "ui_down")
# Deactivation of add_constant_force() does not prevent glitch, but changes the "random" position
add_constant_force(-1000 * delta * direction * transform.basis.x)
# Handle mouse rotation
scaled_mouse_pos_x = (get_viewport().get_mouse_position().x - (get_viewport().size.x / 2)) / (get_viewport().size.x / 2)
scaled_mouse_pos_y = (get_viewport().get_mouse_position().y - (get_viewport().size.y / 2)) / (get_viewport().size.y / 2)
if abs(scaled_mouse_pos_x) >= mouse_range_min:
rot_vel_x = (clampf((abs(scaled_mouse_pos_x) - mouse_range_min), 0, mouse_range_max) * sign(scaled_mouse_pos_x))
else:
rot_vel_x = 0
if abs(scaled_mouse_pos_y) >= mouse_range_min:
rot_vel_y = (clampf((abs(scaled_mouse_pos_y) - mouse_range_min), 0, mouse_range_max) * sign(scaled_mouse_pos_y))
else:
rot_vel_y = 0
global_rotate(Vector3(0,1,0), -rot_vel_x * delta)
rotate_object_local(Vector3(0,0,1), -rot_vel_y * delta)
Thanks in advance!!