MMFeedbacks

repository·master·Indexed 43 days ago

https://github.com/reunono/mmfeedbackspublic

A modular Unity solution for implementing 'game feel' through microinteractions and visual/auditory feedback. It provides a system to trigger effects like screenshake, flashes, and scale changes via the MMFeedbacks component, Unity Events, or C# code. Includes built-in support for regular feedbacks and third-party integrations such as Cinemachine, Post-Processing, and NiceVibrations.

Tokens
2.2K
Snippets
2
Records
8
Agent score
6%

What's inside MMFeedbacks

  1. Fine-tune feedback timing

    master

    Each individual feedback component includes a Timing dropdown in its inspector. This allows you to control the execution flow of that specific feedback using settings such as:

    • Initial Delay: Time to wait before the feedback starts.
    • Cooldown Duration: The minimum time required between two consecutive plays of that feedback.
    • Repeat Options: Configuration for looping or repeating the feedback.
  2. Use the MMFeedbacks component

    master

    The core of the system is the MMFeedbacks component.

    Setup via Inspector

    1. Create an empty GameObject in your scene.
    2. Add the MMFeedbacks component to it.
    3. Set the Initialization Mode (the default Start is recommended for most use cases).
    4. Use the "Add new feedback" dropdown to add specific feedback types (e.g., screenshake, flash).

    Managing Feedbacks

    In the inspector, you can:

    • Reorder: Drag the leftmost icon to change the sequence.
    • Toggle: Use the checkbox to enable or disable individual feedbacks.
    • Rename: Edit the label field.
    • Modify: Fold/unfold feedback settings.
    • Actions: Use the rightmost dropdown to remove or copy feedbacks.
  3. Trigger feedbacks via Unity Events or Code

    master

    Once configured, you can trigger the PlayFeedbacks method using two primary methods:

    1. Via Unity Events

    Drag and drop your MMFeedbacks GameObject into the slot of a Unity Event (such as a UI Button's OnClick event) and select the PlayFeedbacks method.

    2. Via C# Code

    Declare a public MMFeedbacks variable in your script, assign it in the Inspector, and call its methods when needed.

    Available Methods:

    • PlayFeedbacks(): Starts the feedback sequence.
    • Initialize(): Prepares the feedbacks.
    • StopFeedbacks(): Stops currently playing feedbacks.
    • ResetFeedbacks(): Resets feedbacks to their initial state.

    Note: You can also test these methods at runtime directly from the Inspector's bottom button bar or test individual feedbacks using the dropdown on each feedback's title bar.

    using UnityEngine;
    using MoreMountains.Feedbacks;
    
    public class CharacterJump : MonoBehaviour
    {
        // We declare a public MMFeedbacks reference, which we'll set from our CharacterJump inspector
        public MMFeedbacks JumpFeedback;
    
        public void Jump()
        {
          // Trigger the feedback when the jump occurs
          JumpFeedback.PlayFeedbacks();
        }
    }
  4. Install MMFeedbacks in Unity

    master

    To install MMFeedbacks, clone or download this repository and place it into your Unity project folder.

    Handling potential errors: If you see errors after importing, it is likely due to the MMFeedbacksForThirdParties folder. To resolve this, you have two options:

    1. Install dependencies: Use Unity's Package Manager (Window > Package Manager) to install either Cinemachine or PostProcessing effects (or both).
    2. Remove the folder: Delete the MMFeedbacksForThirdParties folder from your project to remove the dependency requirement.
  5. Explore MMFeedbacks via Demos

    master

    The repository includes a demo scene containing all available feedbacks.

    1. Open the MMFeedbacksDemo scene (located in MMFeedbacks/Demos/).
    2. Press Play in the Unity Editor.
    3. Select any feedback in the Hierarchy.
    4. Use the debug Play button in the inspector to trigger the feedback and see it in action.
  6. Use Regular MMFeedbacks

    master

    MMFeedbacks includes a variety of 'Regular' feedbacks that do not require third-party dependencies. Some feedbacks are autonomous, while others require a specific component (often called a 'Shaker') to be present in the scene to function.

    Key regular feedbacks include:

    • Animation: Triggers a bool or trigger parameter on a bound Animator.
    • CameraShake: Broadcasts an MMCameraShakeEvent. Requires an MMCinemachineCameraShaker (or MMCinemachineZoom for Cinemachine) on the camera.
    • CameraZoom: Sets field of view and duration. Requires an MMCameraZoom component on the Camera.
    • Events: Binds Unity events to Play, Stop, Initialization, and Reset methods.
    • Flash: Broadcasts an MMFlashEvent. Requires an MMFlash component on a UI image with a matching FlashID to intercept the event.
    • Flicker: Flickers a specified renderer (sprite, mesh, etc.) with specific color and octave settings.
    • FreezeFrame: Freezes the Unity timescale for a specified duration.
    • InstantiateObject: Instantiates an object at the feedback's position with an optional offset. Supports automatic object pooling if a pool size is specified.
    • Light: Controls the color and intensity of a Light component.
    • ParticlesPlay: Plays an existing ParticleSystem from the scene.
    • ParticlesInstantiation: Instantiates a ParticleSystem at a specified position on Start.
    • Position/Rotation/Scale: Animates target transforms using Vector3 offsets or Transforms, and animation curves.
    • SetActive: Toggles or forces the active state of a target GameObject.
    • Sound: Plays an AudioClip via event (MMSfxEvent), cached (AudioSource), or on demand (instantiated). Supports random volume, pitch, and AudioMixerGroup.
    • TimescaleModifier: Triggers an MMTimeScaleEvent. Requires an MMTimeManager in the scene to modify the timescale.
    • Wiggle: Triggers position, rotation, or scale wiggles on an object equipped with an MMWiggle component.
  7. Use MMFeedbacks for Third-Party Integrations

    master

    Certain feedbacks require third-party libraries to function. If the dependency is missing, these feedbacks will throw errors. For post-processing feedbacks, you must place a corresponding 'Shaker' component on the same object as your Post Process Volume.

    Required Dependencies & Setup:

    • CinemachineImpulse: Requires Cinemachine. Select a Raw Signal preset (e.g., '6D Shake') and requires an MMCinemachineCameraShaker on your virtual camera.
    • Post-Processing Feedbacks: Requires a matching Shaker on the Post Process Volume object:
      • Bloom: Requires MMBloomShaker.
      • ChromaticAberration: Requires MMChromaticAberrationShaker.
      • ColorGrading: Requires MMColorGradingShaker.
      • DepthOfField: Requires MMDepthOfFieldShaker.
      • LensDistortion: Requires MMLensDistortionShaker.
      • Vignette: Requires MMVignetteShaker.
    • Haptics: Requires the NiceVibrations asset.
  8. Create a custom MMFeedback

    master

    To create a new feedback, create a class that inherits from MMFeedback and override the lifecycle methods: CustomInitialization, CustomPlayFeedback, CustomStopFeedback, and CustomReset.

    Use the [FeedbackPath] attribute to define where the feedback appears in the Unity inspector menu.

    using UnityEngine;
    using MoreMountains.Feedbacks;
    
    [AddComponentMenu("")]
    [FeedbackPath("ChosenPath/MyFeedbackNameGoesHere")]
    public class MMFeedbackMyFeedbackNameGoesHere : MMFeedback
    {
        // declare your variables here
    
        protected override void CustomInitialization(GameObject owner)
        {
            base.CustomInitialization(owner);
            if (Active)
            {
                // Put custom initialization code here
            }
        }
    
        protected override void CustomPlayFeedback(Vector3 position, float attenuation = 1.0f)
        {
            if (Active)
            {
                // Put custom play code here
            }
        }
    
        protected override void CustomStopFeedback(Vector3 position, float attenuation = 1)
        {
            base.CustomStopFeedback(position, attenuation);
            if (Active)
            {
                // Put custom stop code here
            }
        }
    
        protected override void CustomReset()
        {
            if (Active)
            {
                // Put custom reset code here
            }
        }
    }