Spaceship moves – mouse/cursor issue

keltwookiekeltwookie Posts: 172Moderator
edited February 2017 in 3D

Having searching everywhere to code spaceships moves, I almost managed to set it properly, but only one thing still annoy me, the mouse moves:

I explain:

The said spaceship is planned to have the same evolutions like Freelancer or X3TC games for those who knows them. For now, here's what I typed:

extends RigidBody

    var X = 0.00
    var Y = 0.00
    var z = 0.00
    var strafe = 0.1 #Player ship strafe speed
    var speed = 1 #Player ship thruster speed
    var rot_ship = 0.25 #Player ship rotation speed

    func _fixed_process(delta):
        #Ship moves + keys assignmnts
        if Input.is_key_pressed(KEY_Z): #strafe up
            translate(Vector3(0, strafe, 0)) 
        if Input.is_key_pressed(KEY_S): #strafe down
            translate(Vector3(0, -strafe, 0))
        if Input.is_key_pressed(KEY_Q): #strafe left
            translate(Vector3(-strafe, 0, 0))
        if Input.is_key_pressed(KEY_D): #strafe left
            translate(Vector3(strafe, 0, 0))
        if Input.is_key_pressed(KEY_X): #strafe backward
            translate(Vector3(0, 0, strafe))
        if Input.is_key_pressed(KEY_W): #move forward/thruster speed
            translate(Vector3(0, 0, -speed))
        if Input.is_key_pressed(KEY_A): #rotate left
            rotate(Vector3(0, 0, 1), deg2rad(-10 * rot_ship))
        if Input.is_key_pressed(KEY_E): #rotate right
            rotate(Vector3(0, 0, 1), deg2rad(10 * rot_ship))

    #############################################################################################
    #Camera motion - In game, the ship should move to the direction indicated by the mouse/cursor in all directions/360 degrees

    func _input(event):

        if event.type == InputEvent.MOUSE_MOTION:
            X += event.relative_x*0.0001

The first part work like a dream but it is the second part which give me headaches, here's the complete section provided by the community here:

unc _input(event):
    #Camera motion
    if event.type == InputEvent.MOUSE_MOTION:
        X += event.relative_x*0.005
        self.set_rotation(Vector3(0, -X, 0))
        Y += event.relative_y*0.005
        if(not Y > 1.5):
            get_node("Camera").set_rotation(Vector3(-Y,0,0))
        else:
            Y = 1.5
        if(not Y < -1.5):
            get_node("Camera").set_rotation(Vector3(-Y,0,0))
        else:
            Y = -1.5
        #Show mouse
        if Input.is_key_pressed(KEY_ESCAPE):
            Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)

I truncated the last part because there is a get_node I don't need. Indeed, in this case, the ship rotate on X axis as it should, but on Y axis, it is the dedicated camera. What must I do to fix this?
...And a detailed answer explaining why would be more than welcome

Thanks by advance

EDIT: BTW, If some find what is above weird or unapropriate, feel free to say it, I'm here to learn :)

Tagged:

Tags :
Sign In or Register to comment.