Ive gotten this far with trying to create an autoaim system for the player. Once Ive gotten the closest target, how do I make the play look that way? I want the player to look that way the moment I hit aim and be able to rotate afterwards like how the autoaim in resident evil 1 (the DS version) works.
elif aim and is_on_floor():
move_speed = 0.0
rotation_speed = 5.0
can_shoot = true
aiming = true
# auto aim
_look.get_closest()
var _closest_target = _look.get_closest()
if _closest_target == CharacterBody3D and _closest_target.damagable and aiming:
rotation.y = move_toward(rotation.y, _closest_target.position.y, 1.0)
# auto aim
_look.get_closest()
var _closest_target = _look.get_closest()
if _closest_target is CharacterBody3D and _closest_target.damagable and aiming:
look_at(_closest_target.global_position)
It still doesnt do anything at all. If I remove the if “_closest_target is CharacterBody3D” part, it goes insane till if looks at something that isnt a CharacterBody3D and crashes.
extends Area3D
func _process(delta: float) -> void:
var closest_body: Node3D = get_closest()
if closest_body:
look_at(closest_body.global_position)
func get_closest() -> Node3D:
var closest_distance: float = INF
var closest_body: Node3D = null
for body in get_overlapping_bodies():
var distance: float = global_position.distance_squared_to(body.global_position)
if distance < closest_distance:
closest_distance = distance
closest_body = body
return closest_body
You can see in the video that the “canon” in the middle switches targets to whichever capsule is closest, and will not move if both of the capsules are outside of range.
See if you can translate that into your project.
My code detect all bodies within the area’s range, so if you need to detect only specific bodies, you should adjust the collision layers and masks appropriately.
I suspect your _closest_target.damagable and aiming variables might also be causing issues, you should print() their values to make sure they are what you expect them to be.
Um… So, it worked at printing the target, the main issue was the view cone I made kept looking at the player cause it was inside the player but after I fixed that, it looks at the closest enemy and prints it but now if I replace that with the “look_at()” which made the player start rotation really fast T~T
Do you have any other code that rotates the player? Like rotate with mouse, or arrow keys, etc. If so, you might need to disable it when aiming, otherwise these 2 systems might be fighting over each other.
I dont think so, but something I noticed is the view cone doesnt rotate with the player so Im not sure why that is. My movement code was from the GDquest tutorial, https://www.youtube.com/watch?v=JlgZtOFMdfc&t=46s&pp=ygUMZ2RxdWVzdCBnb2R0 so Idk if the way movement and rotation is handled by that is different.
One thing I figured out is its been registering the floor as the closest object which Im figuring out how to fix. Its the look_at() thing that I dont understand.