Right now I have two “hacky” solutions with handling this although both use the command and one I found while searching. If the player continues to aim at the enemy while its in alert phase after a certain amount of time it will then transition to dodge
Option 1:
Raycast Sphere in an area where the enemy is near.
Option 2:
Attach a trigger to the ray, similar to a laser from a gun.Once it hits the enemy it will cause alert state until the trigger is moved away. The collider will probably be a cylinder collider scaling up the farther it goes. Similar to a flash light pointer.However it ends up using get component as well.
What’s worse is that unless I want to use alot of else statements I will have to add use scr_EnemyScript for every enemy type and rely on switch cases to distinguish their behaviors.
Option 3:
Check if transform is facing the enemy. Since the transforms rotation is based on the player’s aim is based on the camera center position and not the Player Object, I made the facing target the main camera. This kind of works but fails when the camera is pointing at the ground. If there was a way to fix the angle when looking downwards I will probably go with this one or use a combination which requires more precise aiming at longer distances.
It seems you want to avoid using GetComponent for some reason? If this is because you’ve seen someone warning you to not use GetComponent every frame for performance reasons, this isn’t what they meant. You’re prematurely optimizing, and finding issues that aren’t issues.
That said, if I’m the one implementing this, option 3 would be pretty close to my first choice. You can fix the “fails when pointing at the ground” issue by flattening the vectors involved:
Thank you!. It worked as intended. The magic number for lookCheckAngle was .02 for me. As for the GetComponent avoidance, yeah I read somewhere that I should avoid using getcomponent or findobjectby whenever I can.
GetComponent is quick, it’s just a dictionary lookup I think. FindObjectOfType and GameObject.Find and FindByTag are a slower, as they have to loop through all the objects in your scene. The main reason not to use the latter options though isn’t necessarily speed, but ambiguity - there’s no guarantee that you’ll find the right object named “Foo” if you have accidentally duplicated Foo, that sort of thing. But if that’s the functionality you need, speed should never prevent you from using any of these - none of them will ever be that slow, and many workarounds will add even more slowness.