Best Strategy for Detecting If a Component Has Been Shot
Trying to find best way to detect if a certain component/components have been shot
My assumption would be to ignore all static objects, and only send event to server if not static entity, then server can do checks like seeing if it is Target (Component we want to shoot). Does this sound about right or is there some other features I don't know about to group entities in some way for projectile detection?
If both launcher and Target are client/local then I understand we can just use getComponents to see if it is what we want.
When an entity or player is hit by a projectile, the projectile launcher gizmo fires a related event. These events give you the entity or player that was hit. Then you can send an event to the entity or player if it needs to do a thing upon being hit.
OnProjectileLaunched: CodeBlockEvent<[launcher: Entity]>; OnProjectileHitPlayer: CodeBlockEvent<[playerHit: Player, position: Vec3, normal: Vec3, headshot: boolean]>; OnProjectileHitEntity: CodeBlockEvent<[entityHit: Entity, position: Vec3, normal: Vec3, isStaticHit: boolean]>; OnProjectileHitObject: CodeBlockEvent<[objectHit: Entity, position: Vec3, normal: Vec3]>; OnProjectileHitWorld: CodeBlockEvent<[position: Vec3, normal: Vec3]>; OnProjectileExpired: CodeBlockEvent<[position: Vec3, rotation: Quaternion, velocity: Vec3]>;
https://developers.meta.com/horizon-worlds/reference/2.0.0/core_codeblockevents
Detecting if Correct Component
The best way to find if a certain component/script has been shot (like trying to detect if the players Gun shot a Target) depends on if Gun (which will listen for OnProjectileHitEntity on projectile launcher gizmo) is running in same context or not (both server or both local, or one is server and one is local).Same Context
If both are either server or both local, then we can use getComponent and check if the component exists on the entity that is returned from OnProjectileHitEntity. This is the easiest scenario, and no messaging between scripts is needed.Different Context
If the scripts are not in the same context, then when OnProjectileHitEntity is triggered on Gun component/script it will have to send an event with the entity that was shot (only if is not static, if your shootables are non-static) and ref to self or player (what or who shot it) using broadcastEvent to a server/default script/component like GameManager so that it can then check if the component exists on the entity (Target) and then the GameManager script can send an event back to Gun component/script to say if it hit the Target or not (little more challenging scenario) or if the gun doesn't need to know GameManager can deal with the logic sense it has all the information.Alternative - Use tags
Another way for detecting if its the correct component, is we can add tags on entities, and then all we need to do is check if that tag exists on the entity and we can assume the component is likely on it (unless we did something wrong). This allows us to use tags like "Shootable" and then when we shoot an entity, we check if this tag and if it exists we can trigger our logic. With shootable components, just add the tag on start.