Cursor Animation on Click

Update: You can download the ready-to-use file at the Downloads page named "UI TEMPLATE: CLICK FX" created by the author of AC.

This script provides 3 behaviors to your current cursor in term of better understanding of where is clicked which includes "Hotspot", "NavMesh" and "Elsewhere (anywhere other than Hotspot and Navmesh)". You can watch similar behavior in this video. I created a unitypackage that includes all animations and C# script so you can skip to Step 7, you can download it here.

To use it just follow these:

You'd need to be familiar with Unity's Animation system, but with the aid of a custom Action.

The circle animation(s) would be a prefab that gets spawned in when you click, and which then plays a different animation according to what you clicked on.

1. Add an empty to the scene and attach an Animator. Use Unity's Animation tools to create 3 animations - one for when clicking on the NavMesh, another for when clicking on a Hotspot, and another for when clicking elsewhere. Make this last one the default.
2. For the NavMesh and Hotspot animations, create Trigger parameters that cause these animations to play. Name them "NavMesh" and "Hotspot" respectivly.
3. Make this object a prefab, and remove from the scene.
4. Go to your Project setting's Input manager, and create a new Input named **Click**. Set its **Positive button** to **mouse 0**.
5. Go to AC's Active Inputs window (in the top toolbar) and create a new Active Input. Set its **Input button** to **Click**, and assign an ActionList asset in its **ActionList when trigger** field.
6. Create a new folder in your Project window with your desired name, and copy/paste the code below into a C# file named ActionClickAnim.
7. In the Actions Manager, point to this folder from the "Custom Action scripts" panel to install it as a new **Custom: Click animation** Action.
8. Go to the ActionList created in step 5 and add this new Action to it. Assign the prefab created in step 1, and make sure the Trigger names match.

Here is the code:

```
Using unityengine;
#if unity_editor
using unityeditor;
#endif

      namespace ac
      {

      [system. Serializable]
      public class actionclickanim : action
      {

      public gameobject clickprefab;
      public string onhotspottrigger = "hotspot";
      public string onnavmeshtrigger = "navmesh";
      private gameobject spawnedclickob;
      public float lifetime = 4f;

      public override actioncategory category { get { return actioncategory. Custom; }}
      public override string title { get { return "click animation"; }}

      public override float run ()
      {
      if (clickprefab == null || kickstarter. Playermenus. Ismouseovermenu ())
      {
      return 0f;
      }

      vector3 mousescreenposition = kickstarter. Playerinput. Getmouseposition ();
      vector3 mouseworldposition = camera. Main. Screentoworldpoint (mousescreenposition);
      mouseworldposition. Z = kickstarter. Player. Transform. Position. Z;

      spawnedclickob = instantiate (clickprefab, mouseworldposition, clickprefab. Transform. Rotation);
      destroy (spawnedclickob, lifetime);
      animator _animator = spawnedclickob. Getcomponent<animator> ();

      raycasthit2d hit = physics2d. Raycast (mouseworldposition, vector2. Zero);
      if (hit ! = null && hit. Collider ! = null)
      {
      if (hit. Collider. Getcomponent<hotspot> ())
      {
      _animator. Settrigger (onhotspottrigger);
      }
      else if (hit. Collider. Gameobject. Layer == layermask. Nametolayer ("navmesh"))
      {
      _animator. Settrigger (onnavmeshtrigger);
      }
      }

      return 0f;
      }

      public override void skip () { }

#if unity_editor

public override void showgui ()
{
clickprefab = (gameobject) editorguilayout. Objectfield ("prefab: ", clickprefab, typeof (gameobject), false);
onhotspottrigger = editorguilayout. Textfield ("hotspot trigger: ", onhotspottrigger);
onnavmeshtrigger = editorguilayout. Textfield ("navmesh trigger: ", onnavmeshtrigger);
lifetime = editorguilayout. Floatfield ("lifetime: ", lifetime);
}

#endif

}

}

```