how to set up spaceship controls?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By 1997
:warning: Old Version Published before Godot 3 was released.

I’ve tried everything I know, but the controls are not responsive and the ship just falls(maybe be due to the RigidBody). if anyone could give me some hints, that would be great!

p.s. this is for a 3d game.

Your question needs more information about how exactly you plan to control the ship. via mouse, via keyboard, via combo? Also, showing us how you’re going about your current attempt might help as well.

dc1a0 | 2016-04-27 17:56

well basically I got it to move and the rotation doesn’t effect the forward movement. do I have to use sin cos tan to to calculate the the correct position due to the rotation added?!?If you have any insight please respond

here is the simple code that I got to work.

extends Spatial

func _ready():
    set_process(true);

func _process(delta):

if Input.is_key_pressed(KEY_E):
	var shipPos = get_node("Ship").get_translation()
	shipPos.z = shipPos.z + 5 * delta
	get_node("Ship").set_translation(shipPos)
if Input.is_key_pressed(KEY_Q):
	var shipPos = get_node("Ship").get_translation()
	shipPos.z = shipPos.z - 5 * delta
	get_node("Ship").set_translation(shipPos)
if Input.is_key_pressed(KEY_W):
	var shipRot = get_node("Ship").get_rotation()
	shipRot.x = shipRot.x - 5 * delta
	get_node("Ship").set_rotation(shipRot)

func _enter_tree():
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED);

func _exit_tree():
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE);

1997 | 2016-04-27 22:36

also plan to have “w” key control x rotation, “s” to be opposite rotation of “w”, “D” to control z rotation, “a” opposite rotation of “d”. the “E” key will increase constant forward velocity, while “Q” will decrease constant velocity. that’s the basic idea that I have planed out.

1997 | 2016-04-27 22:42

Well, rotation and velocity are two different things. That’s why changing the rotation does not effect velocity direction. For rotation to affect velocity, velocity has to be rotation * speed, where speed is distance per second * delta unless you want to calculate per frame yourself.

dc1a0 | 2016-04-27 23:14

yes, I understand the issue but how do I translate the velocity(speed) in the direction of the changing rotation?

1997 | 2016-04-27 23:20

Since you said you were using RigidBody:
LinearVelocity = Vector3(shipRot * speed) You may want to tweak that depending on how exactly you want it to move. For instance, to give it a more sliding feel LinearVelocity += Vector3(shipRot * speed) Rotation is already translated into a Vector 3

This is all just off the top of my head, I might have those backwards.

dc1a0 | 2016-04-27 23:31

actually I changed Rigidbody to a spatial so the spaceship wouldn’t just fall when I started the program.

1997 | 2016-04-27 23:51

To stop a rigidbody from falling, turn it’s gravity multiplier to 0. But, for Spatial (Area3D basically,) changing the position would just be you adding current position(translated) + shipRot * speed.

I believe, off the top of my head. If nothing else, that should get you to a place you can figure the rest out.

dc1a0 | 2016-04-28 00:02

Just use _fixed_process(delta) and KinematicBody::move(speed*delta), or _integrate_forces(state) and state.set_{linear,angular}_velocity, instead of changing position in _process.

Bojidar Marinov | 2016-04-28 08:26

well I tried _integrate_forces(state) with rigidbody but the controls only work if i change the mode to character, and it didn’t change help with my original problem(it only moves back and forth on the axis while I want it to fallow the rotation of the ship). also there is another issue, the rotations fallow global rotations which is not what I was going for. what if I just cast a ray in the front of the ship and when I increase speed it just fallows the direction of the ray instead of the axis? but hell if that would even work.

this is the current code, if you have any ideas please let me know.

extends RigidBody

var speed = Vector3(0,0,0)

func _ready():
    set_gravity_scale(0)

func _integrate_forces(state):

    var shipRot = get_node("Ship").get_rotation()

    if Input.is_key_pressed(KEY_E):
	    speed.z = speed.z + 1
    if Input.is_key_pressed(KEY_Q):
	    speed.z = speed.z - 1
   if Input.is_key_pressed(KEY_W):
	    shipRot.y = shipRot.y - 1 
    if Input.is_key_pressed(KEY_S):
	    shipRot.y = shipRot.y + 1 
    if Input.is_key_pressed(KEY_A):
	    shipRot.z = shipRot.z - 1 
    if Input.is_key_pressed(KEY_D):
	    shipRot.z = shipRot.z + 1 

    state.set_linear_velocity(speed)
    state.set_angular_velocity(shipRot)

func _enter_tree():
    Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED);

func _exit_tree():
    Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE);

1997 | 2016-04-28 16:02

:bust_in_silhouette: Reply From: 1997

well after about 10 million tries I’ve found what I was looking for(at least for now). fyi this is in rigidbody.

and here is the code that finally works

extends RigidBody

var speed = 0

func _ready():
   set_fixed_process(true)
   set_gravity_scale(0)

func _fixed_process(delta):

if Input.is_key_pressed(KEY_E):
	speed = speed + .1
if Input.is_key_pressed(KEY_Q):
	speed = speed - .1
if Input.is_key_pressed(KEY_S):
	rotate_x(delta/.8)
if Input.is_key_pressed(KEY_W):
	rotate_x(-delta/.8) 
if Input.is_key_pressed(KEY_A):
	rotate_z(delta/.8) 
if Input.is_key_pressed(KEY_D):
	rotate_z(-delta/.8) 

translate(Vector3(0,0,delta*speed))

func _enter_tree():
    Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED);

func _exit_tree():
    Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE);

also just want to say thanks to Bojidar Marinov and dc1a0 for there help, keep it up.