TargetController - jimdroberts/FishMMO GitHub Wiki
TargetController is a component for FishMMO that controls targeting logic for a character, including raycasting, target selection, and target events. It manages the current and previous targets, updates targeting each frame, and provides events for target changes, updates, and clearing.
-
public const float MAX_TARGET_DISTANCE
The maximum distance allowed for targeting.
-
public const float TARGET_UPDATE_RATE
The update rate (in seconds) for target checks.
-
public LayerMask LayerMask
The layer mask used for target raycasts.
-
public TargetInfo Last
The previous target information.
-
public TargetInfo Current { get; private set; }
The current target information.
#if UNITY_EDITOR
-
public Color GizmoColor
The color used to draw the targeting gizmo in the editor. #endif
-
public TargetInfo Current { get; private set; }
The current target information.
-
public override void OnDestroying()
Called when the object is being destroyed. Clears target events and resets state.
-
void Update()
Updates the target selection each frame, performing raycasts and invoking target events as needed.
-
public TargetInfo UpdateTarget(Vector3 origin, Vector3 direction, float maxDistance)
Updates and returns the TargetInfo for the current target, performing a raycast from the given origin and direction. Parameters: - Vector3 origin: The origin of the ray. - Vector3 direction: The direction of the ray. - float maxDistance: The maximum distance for the raycast. Returns: TargetInfo. The updated target information. No exceptions are thrown.
-
public event Action OnChangeTarget
Event triggered when the target changes.
-
public event Action OnUpdateTarget
Event triggered when the target is updated (but not changed).
-
public event Action OnClearTarget
Event triggered when the target is cleared (e.g., deselected).
- Add the TargetController component to your character GameObject.
- Configure the LayerMask and other fields as needed.
- Subscribe to target events to handle target changes, updates, and clearing.
// Example 1: Subscribing to target events
myTargetController.OnChangeTarget += (target) => { Debug.Log($"Target changed: {target}"); };
myTargetController.OnUpdateTarget += (target) => { Debug.Log($"Target updated: {target}"); };
myTargetController.OnClearTarget += (target) => { Debug.Log($"Target cleared: {target}"); };
// Example 2: Updating the target manually
TargetInfo info = myTargetController.UpdateTarget(origin, direction, 30f);
- Always check for null targets before using them in game logic.
- Use the LayerMask to filter valid targetable objects.
- Subscribe to target events to update UI or trigger gameplay effects.
- Document the intended targeting logic and event usage for maintainability.