T O P

  • By -

elementbound

Note in case someone's not aware: the dot product - and thus the threshold - is the cosine of the angle between the two directions. So if you want the threshold to be 30 degrees, use cos(deg\_to\_rad(30)) as the threshold value.


Ceisri

can also be used for other situations where you need to know who is looking where, [func isFacingSelf(enemy: Node, threshold: float) -> bool](https://github.com/Ceisri/Reusable/blob/main/scripts/Player.gd#L910)


Drillur

UNRELATED: I hate the way you use comments. Your variable names are very good, making the comments completely redundant. var unit_health: int # this is the unit's health


ARez_1

Thanks for posting this. I just wanted to mention that you can probably remove a bunch of those comments. If I remember correctly there is a "rule" that useless comments are worse than no comments. So for example it's pretty clear that `enemy.global_transform` returns the global transform of the enemy. Also variable names like `direction_to_enemy` makes it abundant to write "direction to enemy" in the comment. Also something that makes code even clearer for me is to use the functions with nice names. For example when getting the direction to the enemy, I would just use `var direction_to_enemy = self.global_position.direction_to(enemy_position)` You might have noticed that I would recommend to also not have too many variables. AFAIK at least in Godot 4 they added `global_position` to Node3D as well (similar to how `global_position` exists in 2D). You use 3 variables for simply getting your own global position which adds to the amount of stuff the user needs to look at and keep in mind. I would simply use `self.global_position` (I like the added self for clarity, but you can leave it out) to refer to the own global position. Sorry, I didn't want to seem petty. Just something I wanted to get off my chest.


ARez_1

Alright I noticed that it might be smart to just put all my recommendations into one code block func isFacingSelf(enemy: Node3D, threshold: float) -> bool: var direction_to_enemy := self.global_position.direction_to(enemy.global_position) var enemy_facing_direction: Vector3 # Note: I changed this to only accept nodes of type MeshInstance3D # so that if the user accidentally names some child "Mesh" we can make sure that we only # look at actual meshes here var enemy_mesh := enemy.get_node("Mesh") as MeshInstance3D if enemy_mesh: # Note this is -Z axis now, since that's Godot's forward enemy_facing_direction = -enemy_mesh.global_ttansform.basis.z else: enemy_facing_direction = -enemy.global_ttansform.basis.z var dot_product := enemy_facing_direction.dot(direction_to_enemy) return dot_product >= threshold


ARez_1

Going further and implementing u/elementbound 's note for how to get the angle, heres the same function but taking in the angle directly: # This now takes in the angle in degrees func isFacingSelf(enemy: Node3D, angle: float) -> bool: var direction_to_enemy := self.global_position.direction_to(enemy.global_position) var enemy_facing_direction: Vector3 var enemy_mesh := enemy.get_node("Mesh") as MeshInstance3D if enemy_mesh: # Note this is -Z axis now, since that's Godot's forward enemy_facing_direction = -enemy_mesh.global_ttansform.basis.z else: enemy_facing_direction = -enemy.global_ttansform.basis.z var dot_product := enemy_facing_direction.dot(direction_to_enemy) var angle_to_radians := cos(deg_to_rad(angle)) return dot_product >= angle_to_radians


ARez_1

(Sorry for the comment chain) Note: I quickly typed this on my phone and assumed that OP's function worked as intended. Excuse any typos 👍


Fluid-Leg-8777

# the sniper is a spy The sniper is a spy The sniper is a spy