Could someone tell me what I'm doing wrong, I'll send the scripts.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By akaguriookami
I'm creating an FPS game and in addition to not leaving the bullet when I use the left button, the enemy doesn't disappear.


player script
extends KinematicBody

export var speed = 10
export var acceleration = 5
export var gravity = 0.98
export var jump_power = 20
export var mouse_sensitivity = 0.3

onready var lantern = $Head/Lantern
onready var head = $Head
onready var camera = $Head/Camera

var velocity = Vector3()
var camera_x_rotation = 0

func _ready():
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)

func _input(event):
if event is InputEventMouseMotion:
head.rotate_y(deg2rad(-event.relative.x * mouse_sensitivity))
var x_delta = event.relative.y * mouse_sensitivity
if camera_x_rotation + x_delta > -90 and camera_x_rotation + x_delta <90:
camera.rotate_x(deg2rad(-x_delta))

func _process(delta):
if Input.is_action_just_pressed(“ui_cancel”):
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)

func _physics_process(delta):
var head_basis = head.get_global_transform().basis
var direction = Vector3()
if Input.is_action_pressed(“move_forward”):
direction -= head_basis.z
elif Input.is_action_pressed(“move_backward”):
direction += head_basis.z

if Input.is_action_pressed("move_left"):
		direction -= head_basis.x
elif Input.is_action_pressed("move_right"):
			direction += head_basis.x
			direction = direction.normalized()
		
velocity = velocity.linear_interpolate(direction * speed, acceleration * delta)
velocity.y -= gravity
if Input.is_action_just_pressed("jump") and is_on_floor():
	velocity.y += jump_power

velocity = move_and_slide(velocity, Vector3.UP)


if Input.is_action_just_pressed("lantern"):
	$Head/Camera/SpotLight.visible = not $Head/Camera/SpotLight.visible




enemy script 

extends KinematicBody

export var speed = 200
var health = 100
var space_state
var target

func _ready():
space_state = get_world().direct_space_state

func _process(delta):
if target:
var result = space_state.intersect_ray(global_transform.origin, target.global_transform.origin)
print(result)
if result and result[“collider”].is_in_group(“player”):
look_at(target.global_transform.origin, Vector3.UP)
move_to_target(delta)
if health <= 0:
queue_free()

func _on_Area_body_entered(body):
if body.is_in_group(“player”):
target = body
print(body.name + " entered")

func _on_Area_body_exited(body):
if body.is_in_group(“player”):
target = null
print(body.name + " exited")

func move_to_target(delta):
var direction = (target.global_transform.origin - transform.origin).normalized()
move_and_slide(direction * speed * delta, Vector3.UP)

bullet script

extends RigidBody

const DAMAGE = 50
const SPEED = 100

func _ready():
set_as_toplevel(true)

func shoot(delta):
apply_impulse(transform.basis.z, - transform.basis.z * SPEED)

func _on_Area_body_entered(body):
if body.is_in_group(“Enemies”):
body.health -= DAMAGE
queue_free()
else:
queue_free()

weapon script

extends Node

class_name Weapon

export var fire_rate = 0.5
export var clip_size = 20
export var reload_rate = 1
export var default_position : Vector3
export var ads_position : Vector3
export var ads_accelaration : float = 0.3
export var default_fov : float = 70
export var ads_fov : float = 55

onready var muzzle = $muzzle
onready var bull = preload(“res://Player/Bullet.tscn”)
onready var ammo_label = $“/root/World/UI/Label”
export var raycast_path : NodePath
export var camera_path : NodePath

var raycast : RayCast
var camera : Camera

var current_ammo = clip_size
var can_fire = true
var reloading = false
func _ready():
current_ammo = clip_size
raycast = get_node(raycast_path)
camera = get_node(camera_path)

func _process(delta):
if reloading:
ammo_label.set_text(“Reloading…”)
else:
ammo_label.set_text(“%d / %d” % [current_ammo, clip_size])

func _physics_process(delta):
if Input.is_action_pressed(“primary_fire”) and can_fire:
if raycast.is_colliding():
var bullet = bull.instance()
muzzle.add_child(bullet)
bullet.look_at(raycast.get_collision_point(), Vector3.UP)
bullet.shoot(delta)
can_fire = false
yield(get_tree().create_timer(0.25), “timeout”)
can_fire = true

	if current_ammo > 0 and not reloading:
		fire()
	elif not reloading:
		reload()
		
if Input.is_action_just_pressed("reload") and not reloading:
	reload()
	

func check_collision():
var collider = raycast.get_collider()
if raycast.is_colliding():
if collider.is_in_group(“Enemies”):
print(“killed” + collider.name)
collider.queue_free()

func fire():
print(“fired weapon”)
can_fire = false
current_ammo -= 1
yield(get_tree().create_timer(fire_rate), “timeout”)
can_fire = true

func reload():
print(“reloading”)
reloading = true
yield(get_tree().create_timer(reload_rate), “timeout”)
current_ammo = clip_size
reloading = false
print(“reload complete”)

shotgun script

extends Weapon

export var fire_range = 10

func _ready():
raycast.cast = Vector3(0, 0, -fire_range)

The reason you didn’t get an answer is:

  1. The title is not specific.
  2. Your question is too vague.
  3. The formatting is not easy to read. Highlight the code and click the curly braces symbol above.
  4. There’s way too much code here to expect anyone to reasonably trawl through. Just add what’s relevant.

If you bear these points in mind in future then you’re much more likely to get an answer.

DaddyMonster | 2021-12-02 15:40