Topic was automatically imported from the old Question2Answer platform.
Asked By
CheddarBear
I am trying to make a 3D spaceship to move I got it to where the I can roll, pitch, and yaw but when I roll it does not pitch or yaw like it should, it feels as if the pitch and yaw are stuck on the global axis even though they shouldn’t be. I’ve been searching and googling for around 2 weeks now and haven’t fixed it.
My code for the ship:
extends KinematicBody
var turn_right = false
var turn_left = false
export (int) var rot_speed = 35
export (int) var turn_speed = 35
export (int) var speed = 100
var velocity
var camera_angle = 0
export (float) var mouse_sensitivity = 0.4
var rot_x = 0.0
var rot_y = 0.0
var rot_z = 0.0
onready var movementGuide = $MovementGuide
func get_input(delta):
velocity = 0
if Input.is_action_pressed("Forward"):
velocity = speed
if Input.is_action_pressed("Backward"):
velocity = -speed
if Input.is_action_pressed("Right"):
rot_y += -turn_speed * delta
turn_right = true
if Input.is_action_pressed("Left"):
rot_y += turn_speed * delta
turn_left = true
if Input.is_action_pressed("RotateRight"):
rot_z += -rot_speed * delta
if Input.is_action_pressed("RotateLeft"):
rot_z += rot_speed * delta
func _input(event):
if event is InputEventMouseMotion:
rot_x += (deg2rad(-event.relative.y * mouse_sensitivity))
event.relative = Vector2()
func _physics_process(delta):
get_input(delta)
rotation_degrees.x = rot_x
rotation_degrees.y = rot_y
rotation_degrees.z = rot_z
move_and_slide(-transform.basis.z * velocity * delta)
I found the problem thank you i had to use “rotate_object_local” instead of “rotation_degrees”
so my code is:
Ignore all the other stuff not about rotation i was tryna implement acceleration and animation
extends KinematicBody
var turn_right = false
var turn_left = false
var turn_up = false
var turn_down = false
export (int) var rot_speed = 100
export (int) var turn_speed = 100
export (int) var speed = 0
export (int) var accel = 0
export (int) var maxSpeed = 1200
export (int) var minSpeed = -600
export (int) var rot_multi = 15
export (int) var turn_multi = 15
var velocity
var angle_v_adjust = 0
onready var camera = $"res://Prefabs/Ship/Ship/Camera"
onready var anim = $"res://Prefabs/Ship/Ship/AnimationPlayer"
var camera_angle = 0
export (float) var mouse_sensitivity = 4
var mrot_y = 0.0
var rot_x = 0.0
var rot_y = 0.0
var rot_z = 0.0
onready var movementGuide = $MovementGuide
func get_input(delta):
velocity = 0
if Input.is_action_pressed("Forward"):
speed += accel
elif Input.is_action_pressed("Backward"):
speed -= accel
else:
speed -= accel * 1/5
if Input.is_action_pressed("Right"):
rot_y = -turn_speed * delta * turn_multi
turn_right = true
elif Input.is_action_pressed("Left"):
rot_y = turn_speed * delta * turn_multi
turn_left = true
else:
turn_left = false
rot_y = 0
if Input.is_action_pressed("RotateRight"):
rot_z = -rot_speed * delta * rot_multi
elif Input.is_action_pressed("RotateLeft"):
rot_z = rot_speed * delta * rot_multi
else:
rot_z = 0
func _input(event):
if event is InputEventMouseMotion:
rot_x = 0.0
rot_x += deg2rad(-event.relative.y * mouse_sensitivity)
mrot_y = 0.0
mrot_y += deg2rad(-event.relative.x * mouse_sensitivity)
event.relative = Vector2()
#transform.basis = Basis()
rotate_object_local(Vector3(1, 0, 0), deg2rad(rot_x))
rotate_object_local(Vector3(0, 1, 0), deg2rad(mrot_y))
func _physics_process(delta):
if speed < maxSpeed:
accel += delta * 5
if speed < 0:
accel += delta * 1/3
get_input(delta)
get_input(delta)
#rotation_degrees.x = rot_x
#rotate_object_local(Vector3(1, 0, 0), deg2rad(rot_x))
#rotate_object_local(Vector3(0, 1, 0), deg2rad(mrot_y))
rotate_object_local(Vector3(0, 1, 0), deg2rad(rot_y))
rotate_object_local(Vector3(0, 0, 1), deg2rad(rot_z))
#rotation_degrees.z = rot_z
if speed > 0:
speed = min(speed, maxSpeed)
elif speed < 0:
speed = max(minSpeed, speed)
move_and_slide(-transform.basis.z * speed * delta)
How’s your adventure going?
I recently started implementing a 6-axis system to a ship myself … BOY OH BOY. Quite the mind bender, hopefully I’ll get to a point where this code comes in handy. Thank you so much for sharing your experience!