0 votes

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.

in Engine by (81 points)
edited by

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.

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);

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.

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.

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

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.

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

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.

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.

well I tried integrateforces(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);

1 Answer

0 votes

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.

by (81 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.