diff --git a/src/character_actor.cpp b/src/character_actor.cpp index 7eddd2c..3886334 100644 --- a/src/character_actor.cpp +++ b/src/character_actor.cpp @@ -65,8 +65,12 @@ void CharacterActor::move(Vector3 world_vector) { void CharacterActor::aim(Vector3 at) { // calculate the forward vector by normalized difference between player character and the target on the XZ plane - Vector3 const position{this->weapon_muzzle->get_global_position()}; - Vector3 const forward{Vector3{at.x, 0.f, at.z} - Vector3{position.x, 0.f, position.z}}; + Vector3 const pos_flat{this->get_global_position().x, 0.f, this->get_global_position().z}; + Vector3 const target_flat{at.x, 0.f, at.z}; + Vector3 const muzzle_flat{this->weapon_muzzle->get_global_position().x, 0.f, this->weapon_muzzle->get_global_position().z}; + if(pos_flat.distance_squared_to(target_flat) < pos_flat.distance_squared_to(muzzle_flat)) + return; + Vector3 const forward{target_flat - muzzle_flat}; this->aim_direction(forward.normalized()); }