Sensing Component 2D Documentation

 SensingComponent2D Documentation

1. Introduction


This document provides a comprehensive guide for setting up and using the SensingComponent2D in Unity. The SensingComponent2D allows objects to detect other objects within a vision cone and respond to noise events, making it suitable for implementing AI perception in your game.

2. Setup Guide

2.1 Prerequisites


Before you begin, ensure you have the following:


    Unity 2022.3.22f1 or later

    Basic understanding of C# and Unity development


2.2 Installation


    Download the SensingComponent2D files: Ensure you have the SensingComponent2D.cs script.

    Add Script to Unity Project: Place the downloaded script in the appropriate folder within your Unity project's Assets directory.


2.3 Configuration


    SensingComponent2D Initialization: Add the SensingComponent2D script to a GameObject in your scene to enable vision and hearing capabilities.


3. Script Reference

3.1 SensingComponent2D Class


The SensingComponent2D class provides functionalities for detecting objects through vision and hearing.

Properties


    bool isVisionEnabled: Enables or disables vision detection.

    float sensingInterval: Interval between sensing checks.

    float perceptionRadius: Radius of the vision cone.

    float perceptionAngle: Angle of the vision cone.

    LayerMask detectionLayer: Layer mask to filter detectable objects.

    LayerMask obstacleLayer: Layer mask to filter obstacles.

    List<GameObject> _detectedObjects: List of currently detected objects.

    bool isHearingEnabled: Enables or disables hearing detection.

    float hearingThreshold: Maximum volume threshold for hearing noise.


Events


    System.Action<GameObject> OnVisionObjectDetected: Event triggered when a game object is seen.

    System.Action<GameObject> OnVisionObjectForgotten: Event triggered when a previously seen game object is no longer in vision.

    System.Action<GameObject, Vector3, float> OnNoiseHeard: Event triggered when noise is heard.


Methods


    static void ReportNoise(GameObject instigator, Vector3 location, float volume): Reports a noise event to all SensingComponent2D instances in the scene.

    void StartVisionDetection(): Starts the vision detection routine.

    void StopVisionDetection(): Stops the vision detection routine.

    void SetInterval(float newInterval): Sets a new sensing interval.

    void SetDetectionLayer(LayerMask newMask): Sets a new detection layer mask.

    void SetObstacleLayer(LayerMask newMask): Sets a new obstacle layer mask.

    void SetEnableHearing(bool newValue): Enables or disables hearing detection.

    void SetMaxHearingVolume(float newValue): Sets the maximum volume threshold for hearing noise.


3.2 Example Usage


Reporting Noise:

SensingComponent2D.ReportNoise(gameObject, transform.position, volume);


Starting and Stopping Vision Detection:

SensingComponent2D sensingComponent = aiCharacter.GetComponent<SensingComponent2D>();

sensingComponent.StartVisionDetection();

sensingComponent.StopVisionDetection();



3.3 Debugging


The OnDrawGizmos method provides visual debugging for the vision cone and detection area. This helps in visualizing the detection area in the Unity Editor.

4. Conclusion


The SensingComponent2D is a versatile tool for adding vision and hearing capabilities to your AI characters in Unity. By following the setup guide and utilizing the provided script reference, you can effectively integrate and customize the SensingComponent2D to fit your game's requirements.

Comments

Popular posts from this blog

Sensing Component Documentation

Documentation for Modular Female Character System