+2 votes

I'm trying to get the camera to move a bit when the ship moves and go back to its original position when not moving very similar to this video.
https://www.youtube.com/watch?v=ijh-n-ntImA (0:00:20-0:01:17)
I've tried multiple times, but I can barley get the camera to move where I want it to be.
If you have any suggestions please leave a comment

for reference this is the code for the ship

extends RigidBody

var speed = 1
var isshooting = false
var laserCount = 0
var laser = preload("res://assests/models/laser/laser.scn")
var laserArray = []
var camPos = Vector3(0,0,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):
    get_node("Ship").rotate_x(delta/.8)
    if camPos.x < .1:
        camPos.x += .01
        get_node("Ship/Camera").translate(Vector3(0,camPos.x,0))
    else:
        camPos.x -= .02
        get_node("Ship/Camera").translate(Vector3(0,camPos.x,0))
if Input.is_key_pressed(KEY_W):
    get_node("Ship").rotate_x(-delta/.8) 
if Input.is_key_pressed(KEY_A):
    get_node("Ship").rotate_z(delta/.7) 
if Input.is_key_pressed(KEY_D):
    get_node("Ship").rotate_z(-delta/.7) 

if speed > 10:
    speed = 10
get_node("Ship").translate(Vector3(0,0,delta*speed))

if Input.is_action_pressed("ui_space"):
    if isshooting == false:
        fire()
        isshooting = true
else:
    isshooting = false

var laserid = 0
for laser in laserArray:
    get_node(laser).translate(Vector3(0,0,delta * (speed*speed)/6 +.2 ))
    if get_node(laser).get_translation().x > 50:
        remove_child(get_node(laser))
        laserArray.remove(laserid)
    elif get_node(laser).get_translation().z > 50:
        remove_child(get_node(laser))
        laserArray.remove(laserid)
    elif get_node(laser).get_translation().y > 50:
        remove_child(get_node(laser))
        laserArray.remove(laserid)
    laserid += 1


func fire():
    laserCount = laserCount + 1
    print("Fire!")
    var laser_instance = laser.instance()
    laser_instance.set_name("laser" + str(laserCount))
    add_child(laser_instance)
    laser_instance.set_translation(get_node("Ship").get_translation())
    laser_instance.set_rotation(get_node("Ship").get_rotation())
    laserArray.push_back("laser" + str(laserCount))
    print(laserArray)


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

func _exit_tree():
    Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE);
in Engine by (83 points)

would it be possible to delay the movement of the camera from this ship?

well I almost got it working...I can seem to stop the camera from flying way off of the ship nor does the camera go back to it's original position.

this is the part of the code that I have been working on.

extends RigidBody

var speed = 1
var isshooting = false
var laserCount = 0
var laser = preload("res://assests/models/laser/laser.scn")
var laserArray = []
var delayArray = [Vector3(0,0,0),Vector3(0,0,0), Vector3(0,0,0)]
var camMov = Vector3(0,0,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):
    get_node("whole").rotate_x(delta/.8)
    if camMov.y > .1:
        camMov.y -= .01
    else:
        camMov.y += .01
elif not camMov.y == 0:
    camMov.y -= .01
if Input.is_key_pressed(KEY_W):
    get_node("whole").rotate_x(-delta/.8) 
    if camMov.y > .1:
        camMov.y += .01
    else:
        camMov.y -= .001
elif not camMov.y == 0:
    camMov.y += .01
if Input.is_key_pressed(KEY_A):
    get_node("whole").rotate_z(delta/.7) 
    if camMov.x > .1:
        camMov.x -= .01
    else:
        camMov.x += .01
elif not camMov.x == 0:
    camMov.x -= .001
if Input.is_key_pressed(KEY_D):
    get_node("whole").rotate_z(-delta/.7) 
    if camMov.x > .1:
        camMov.x += .01
    else:
        camMov.x -= .01
elif not camMov.x == 0:
    camMov.x += .01

if speed > 10:
    speed = 10
get_node("whole/ship").translate(Vector3(0,0,delta*speed))

delayArray.push_back(Vector3(0,0,-delta*speed) + Vector3(camMov))
get_node("whole/Camera").translate(Vector3(delayArray[0]))
delayArray.pop_front()

If you have any ideas just let me know.

Is there a way to do this with animation? because I can't do it with coding.

I've moved on to trying the same idea but with animation, wish me luck.

It is just incredible how much spam this question and its answer got... (all is hidden now, but still)

1 Answer

+1 vote

here is the code that worked for me

if Input.is_action_pressed("ui_down"):
    get_node("Ship").rotate_x(delta/.8)
    if ship.origin.y < .8:
        up = .01
    else:
        up = 0
elif ship.origin.y > .51:
    up = -.01
else:
    up = 0

up is the speed at which the camera moves

ship is the current transform of the camera

and then say

get_node("Ship/Camera").translate(Vector3(right + left, up + down,0))

that is how I did it.

by (83 points)

I appreciate this article. I was following and success
nodejs quiz , flip diving , bottle flip

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.