Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Make object animate when mouse hovers over the top in 2D game

Discussion in '2D' started by zaraewilkins, Aug 3, 2019.

  1. zaraewilkins

    zaraewilkins

    Joined:
    Aug 3, 2019
    Posts:
    2
    Im new to unity and wanted to find out if this effect was possible and how to do it.

    Say you had an object on the ground in your 2D game. I want to make it so when you hover the mouse over the object it will animate (otherwise the object would be still). For example if you wanted to give it a glow, or make it vibrate or change colour or something like that.

    Anyone know how to do this?
     
  2. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    A little bit of googling can find a solution for almost everything:
    https://answers.unity.com/questions/547513/how-do-i-detect-when-mouse-passes-over-an-object.html

    Give the object a collider, and you can use OnMouseOver/OnMouseExit in a MonoBehaviour script attached to it.
    Code (CSharp):
    1.  using UnityEngine;
    2.  
    3. public class Example : MonoBehaviour
    4. {
    5.     void OnMouseOver()
    6.     {
    7.         // do mouse hover stuff
    8.     }
    9.  
    10.    void OnMouseExit()
    11.    {
    12.        // reset to normal
    13.    }
    14. }
    From there you can do whatever you like, turn on another sprite, run an animation, change colors, etc.
     
    Lotoos, vakabaka and zaraewilkins like this.
  3. zaraewilkins

    zaraewilkins

    Joined:
    Aug 3, 2019
    Posts:
    2

    Ah thanks so much. Im not at all familiar with code, but is there any where that can tell me what those different things mean and how to change them to what I need? Do you think that it is not really possible to use Unity without knowing code?

    Thanks so much for your help.
     
  4. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    Here's a heavily commented version of that code:
    Code (CSharp):
    1. using UnityEngine; // lets you access MonoBehaviour, GetComponent, and more
    2.  
    3. // This is a class named Example that inherits from MonoBehaviour, which gives it
    4. // access to lots of events and useful functions, and means it is a Component, able to be
    5. // attached to a GameObject in the Unity Editor
    6.  
    7. public class Example : MonoBehaviour
    8. {
    9.   // This is a MonoBehaviour-provided function that Unity will run one time
    10.   // automatically when the cursor is over this GameObject
    11.   // (GameObject needs a collider component)
    12.  
    13.   void OnMouseOver()
    14.   {
    15.       // in here you write code to respond to this event
    16.  
    17.       // Use GetComponent to find the SpriteRenderer component on this GameObject
    18.       // then set its color to be green
    19.       GetComponent<SpriteRenderer>().color = Color.green;
    20.   }
    21.  
    22.   // This is a MonoBehaviour-provided function that Unity will run one time
    23.   // automatically when the cursor is no longer over this GameObject
    24.   // (GameObject needs a collider component)
    25.  
    26.    void OnMouseExit()
    27.    {
    28.       // in here you write code to respond to this event
    29.  
    30.       // Use GetComponent to find the SpriteRenderer component on this GameObject
    31.       // reset it to normal color
    32.       GetComponent<SpriteRenderer>().color = Color.white;
    33.    }
    34. }
    I don't think that it's impossible to use Unity without coding, but you'll be very limited to what you can create.

    You might be interested in checking out Unity's 2D Game Kit, which has a lot more components and tools to create actual gameplay with.

    https://learn.unity.com/project/2d-game-kit
     
    Last edited: Aug 5, 2019