0

I'm trying to make the player shoot bullets out

The thing is, they're not pointing in the same direction as the player

They also do not follow the player as they move around

code for the bullet

I store the object that shot the bullet so that the bullet can signal back if it has hit something

extends KinematicBody

var time = 0
var speed = 1800
var velocity = Vector3()
var collision = NAN
# stores the object that shot it
var shooter = NAN


func start(own):
    shooter = own
    # TODO Finish
    global_transform.basis = own.global_transform.basis
    velocity = own.global_transform.basis.z * speed + own.velocity

func _process(delta):
    collision = move_and_collide(velocity * delta)
    if collision:
        # get parent of the StaticBody or RigidBody
        print(collision.collider.get_parent())
        queue_free()

    time += delta
    if time > 3:
        queue_free()

Code for the Player

extends KinematicBody

var velocity = Vector3()
var acceleration = 1
var retrograde = false

var mouse_sensitivity = 0.008
var rot_x_target = 0
var rot_y_target = 0
var rot_x_speed = 0
var rot_y_speed = 0
var rot_acceleration = 7
var max_rot_x_speed = 0
var max_rot_y_speed = 0

var move_camera = false     # if true, the 3rd person camera will move arnd freely

var bullet = preload("res://18 3 red round.tscn")
var reload_time = 0.2
var reload_timer = 0
var fire_gun = false

func shoot():
    var bullet_node = bullet.instance()
    bullet_node.start(self)
    get_parent().add_child(bullet_node)

# to keep track of what buttons are pressed
var pressed = {}

func is_action_just_pressed(action):

    if Input.is_action_pressed(action):
        if not pressed.has(action) or not pressed[action]:
            pressed[action] = true
            return true
    else:
        pressed[action] = false
    return false


func _physics_process(delta):
    # check if the deccelerate command is active
    # if so, wait for the current rotation to finish
    if retrograde and abs(rot_x_speed) < rot_acceleration * delta and abs(rot_y_speed) < rot_acceleration * delta:
        # orders the ship to flip
        rot_x_target = PI
        retrograde = false

    # IMPULSOR below
    # constantly calculates the maximum rotational speed to reach the target orientation
    if rot_x_target != 0:
        max_rot_x_speed = sqrt(2 * abs(rot_x_target) * rot_acceleration) * abs(rot_x_target) / rot_x_target

    if rot_y_target != 0:
        max_rot_y_speed = sqrt(2 * abs(rot_y_target) * rot_acceleration) * abs(rot_y_target) / rot_y_target

    # controls the speed in each axes to be equal to the max rotational speed
    # if the difference is greater than the acceleration, a correction is made
    if abs(max_rot_x_speed - rot_x_speed) > rot_acceleration * delta:
        if rot_x_speed < max_rot_x_speed:
            rot_x_speed += rot_acceleration * delta
        elif rot_x_speed > max_rot_x_speed:
            rot_x_speed -= rot_acceleration * delta

    if abs(max_rot_y_speed - rot_y_speed) > rot_acceleration * delta:
        if rot_y_speed < max_rot_y_speed:
            rot_y_speed += rot_acceleration * delta
        elif rot_y_speed > max_rot_y_speed:
            rot_y_speed -= rot_acceleration * delta

    rotate_object_local(Vector3(1, 0, 0), - rot_x_speed * delta)
    rotate_object_local(Vector3(0, 1, 0), rot_y_speed * delta)
    rot_x_target -= rot_x_speed * delta
    rot_y_target -= rot_y_speed * delta

    reload_timer += delta   # for incrementing the reload timer
    if reload_timer >= reload_time and fire_gun:
        shoot()
        reload_timer = 0

    move_and_slide(velocity)


func _input(event):
    if event.is_action("midmouse"):
        move_camera = not move_camera

    if event.is_action_pressed("retrograde"):
        retrograde = true

    if event.is_action("shoot"):
        fire_gun = not fire_gun

    if event is InputEventMouseMotion and not move_camera and not retrograde:
        rot_x_target -= event.relative.y * mouse_sensitivity
        rot_y_target -= event.relative.x * mouse_sensitivity

    elif event.is_action("move_forward"):
        velocity += get_global_transform().basis.z * acceleration

https://youtu.be/29ynaoqyM1k is a vid of the game not working

Manglemix
  • 3
  • 1

1 Answers1

0

Okay I found out that the scene was rotated 90 degrees on the x-axis for some reason

And that you can copy the global_transform directly

Manglemix
  • 3
  • 1