Godot Version
4.2
Question
Why is this not working
if Input.is_action_just_pressed("fire") or Input.is_action_pressed("fire") :
if Aimcast.is_colliding():
var target = Aimcast.get_collider()
if target.is_in_group("ENEMY"):
print("hit enemy")
target.health -= damage
Try printing target to see if it’s null
1 Like
Can you show the entire code?
1 Like
hi, here is the entire code for the player
extends CharacterBody3D
const BASE_FOV = 100.0
const FOV_CHANGE = 3.5
var speed
var CROUCH_SPEED = 3
var crouch_speed = 3
const WALK_SPEED = 6.2
const SPRINT_SPEED = 8.0
const JUMP_VELOCITY = 5.5
const sensitvity = 0.01
const SPEED_SPRINT = 10.0
var default_height = 1.5
var crouch_height = 0.3
const bob_freq = 2.3
const bob_amp = 0.08
var t_bob = 0.0
var damage = 100
var instance
@onready var head = $Neck
@onready var camera = $Neck/Camera3D
@onready var collision = $CollisionShape3D
@onready var headbonker = $HeadBonker
@onready var Aimcast = $Neck/Camera3D/hand/AimCast
@onready var animshoot = $AnimationPlayer
var gravity = 9.8
func _enter_tree():
set_multiplayer_authority(name.to_int())
func _ready():
if not is_multiplayer_authority(): return
camera.current = true
func _unhandled_input(event):
if event is InputEventMouseMotion:
head.rotate_y(-event.relative.x * sensitvity)
camera.rotate_x(-event.relative.y * sensitvity)
camera.rotation.x = clamp(camera.rotation.x, deg_to_rad(-40),deg_to_rad(60) )
if event is InputEventMouseButton:
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
elif event.is_action_pressed("ui_cancel"):
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
func fire():
if Input.is_action_pressed("fire"):
if not animshoot.is_playing():
animshoot.play("Shoot")
func _physics_process(delta):
if is_multiplayer_authority():
fire()
if Input.is_action_just_pressed("fire"):
if Aimcast.is_colliding():
var target = Aimcast.get_collider()
if target.is_in_group("ENEMY"):
print("hit enemy")
target.health -= damage
if Input.is_action_just_pressed("quit"):
$"../".exit_game(name.to_int())
get_tree().quit()
#print(stanima)
if not is_on_floor():
velocity.y -= gravity * delta
if headbonker.is_colliding():
collision.shape.height -= crouch_speed * delta
collision.shape.height = clamp(collision.shape.height, crouch_height, default_height)
# Handle jump.
if is_on_floor() or is_on_wall():
if Input.is_action_just_pressed("Jump"):
velocity.y = JUMP_VELOCITY
if Input.is_action_pressed("sprint"):
speed = SPRINT_SPEED
elif headbonker.is_colliding():
speed = CROUCH_SPEED
else:
speed = WALK_SPEED
if Input.is_action_pressed("crouch"):
speed = CROUCH_SPEED
collision.shape.height -= crouch_speed * delta
else:
collision.shape.height += crouch_speed * delta
collision.shape.height = clamp(collision.shape.height, crouch_height, default_height)
# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.
var input_dir = Input.get_vector("left", "right", "forward", "back")
var direction = (head.transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
if is_on_floor():
if direction:
velocity.x = direction.x * speed
velocity.z = direction.z * speed
else:
velocity.x = lerp(velocity.x, direction.x * speed, delta * 7.0)
velocity.z = lerp(velocity.z, direction.z * speed, delta * 7.0)
else:
velocity.x = lerp(velocity.x, direction.x * speed, delta * 2.0)
velocity.z = lerp(velocity.z, direction.z * speed, delta * 2.0)
t_bob += delta * velocity.length() * float(is_on_floor())
camera.transform.origin = _headbob(t_bob)
#fov
var velocity_clamped = clamp(velocity.length(), 0.5, SPRINT_SPEED * 2)
var target_fov = BASE_FOV + FOV_CHANGE * velocity_clamped
camera.fov = lerp(camera.fov, target_fov, delta * 8)
move_and_slide()
func _headbob(time) -> Vector3:
var pos = Vector3.ZERO
pos.y = sin(time * bob_freq) * bob_amp
pos.x = cos(time * bob_freq / 2) * bob_amp
return pos
You are going to have to elaborate a bit.
How far into this block of if’s does it go? Does it make it past the first if statement?
PS: That if statement doesn’t really make sense. is_action_just_pressed() fires when the key is first pressed down. It is usually used to avoid the multiple hits you get from using is_action_pressed().
If your goal is to let the user hold the button for continuous fire then use is_action_pressed()
If your goal is to single shot the weapon then use is_action_just_pressed()
2 Likes
Try change this:
To this:
if Input.is_action_just_pressed("fire") or Input.is_action_pressed("fire") :
var target = Aimcast.get_collider()
if target and target.is_in_group("ENEMY"):
print("hit enemy")
target.health -= damage
You’re not checking to see if target is a valid object before referencing it.