The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates.

godotengine.org | Twitter

0 votes

I am asking this because I didn't find any example how to make an enemy firing a bullet at the player in Godot (just the other way round). I am also pretty new to Godot.

This is what I've tried so far:

(Bot1 (KinematicBody Enemy node):

var fire_delay
var max_fire_delay = .25

var bot1_laser_scene = preload("res://Enemies/Bot1_Laser.tscn")

func _ready():
    fire_delay = max_fire_delay
    add_to_group("Enemy")

func _process(delta):
    var playerPosition = get_node("../Player").get_translation()
    look_at(playerPosition, UP)
    var follow_vect = (playerPosition-get_translation()).normalized()*delta*speed
    move_and_slide(follow_vect)
    shoot(delta)

func shoot(delta):
    if fire_delay < 0:
        var shot = bot1_laser_scene.instance()
        shot.global_transform.origin = global_transform.origin
        shot.rotation = rotation
        get_tree().root.add_child(shot)
        fire_delay = max_fire_delay
    else:
        fire_delay -= 1.0 * delta

(Bot1Laser (KinematicBody Enemy's Bullet (Bot1Laser) node):

extends KinematicBody
var ttl = 1.5
var speed = 2

func _ready():
    pass

func _process(delta):
    if ttl < 0:
        queue_free()
    else:
        ttl -= delta
        var follow_vect = get_translation().normalized()*delta*speed
        translate(follow_vect)

The bots are facing into the players direction, and the shoot function is triggered regularly,
however there are not bullets fired. Any idea whats wrong here?

in Engine by (16 points)

1 Answer

0 votes

you haven't added the children of instance scene. Here you go:

extends RigidBody

const Z_FRONT = -1 #in this game the front side is towards negative Z

var THRUST_Z = 2000

var power_throw = 0
var speed_value = 0

const THRUST_TURN = 100  #2200 

export var player_pos = Vector3()
export var ray_length = 10000000

export var mouse_pos = Vector2()

var camera
export var from = Vector3()
export var to = Vector3()
var space_state
var result

var holder = Vector3()
var holder2

var op = preload("res://Scenes/SceneObjects/RigidBallInstance.tscn").instance()

var minimap_icon = "player"
var look_vector = Vector3()

var carried_object = null

var followed_object = null

var a_body_name

#onready var leftEngineParticles = $ParticlesLeftEngine
#onready var rightEngineParticles = $ParticlesRightEngine
onready var cameravi = get_node("/root/Node/CameraGimbal/InnerGimbal/Camera")


#onready var cameravi = get_node("/root/Node/Player/Spatial/InterpolatedCamera")
var target

var lookat2

var biggerball
var speedy_one

#var wasThrust = false #particles enabled?
# damping: see linear and angular damping parameters
#onready var Pause_Menu = get_node("/root/Node_PauseMenu")
func _ready():
    pass


func power_throwing(_delta):

    if Input.is_action_pressed("right_mouse"):
        if power_throw <= 250:
            print(power_throw)
            power_throw += .1
            biggerball.scale = Vector3(power_throw, power_throw, power_throw)
    if Input.is_action_pressed("left_mouse"):
        get_node("..").add_child(op)
        op.scale = Vector3(0.1,0.1,0.1)
        op.mass = 1
        op.angular_damp = -1
        op.linear_damp = 0.18
        op.transform.origin = Vector3(lookat2.get_global_transform().origin.x, lookat2.get_global_transform().origin.y + 5, lookat2.get_global_transform().origin.z)
        look_vector = (Vector3(result.position.x, result.position.y, result.position.z) - cameravi.get_global_transform().origin).normalized()
        op.apply_impulse(Vector3(), get_node(".").look_vector * Vector3(333, 333, 333))
by (193 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.