- Home /
Enlarge GameObject when I mouse over the specific object
How do I make an object bigger when I put the cursor on it? I'm making a card game, so I want my card to enlarge when I'm hovering it over; kind of like in Hearthstone. I'm pretty new to C#.
Thank you in advance
Answer by SteenPetersen · Aug 20, 2017 at 07:31 PM
Hi there,
place a collider on the card and use: https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnMouseOver.html
then you can simply increase its scale:
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour
{
void Example()
{
// Widen the object by 0.1
transform.localScale += new Vector3(0.1F, 0, 0);
}
}
https://docs.unity3d.com/ScriptReference/Transform-localScale.html
@SteenPetersen Hello and thank you for you quick response!
Although I added a collider to my object, nothing is happening. Here's my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class $$anonymous$$ouseOver : $$anonymous$$onoBehaviour {
public void On$$anonymous$$ouseEnter()
{
transform.localScale += new Vector3(0.1F, 0, 0);
}
public void On$$anonymous$$ouseOver()
{
transform.localScale += new Vector3(0.1F, 0, 0);
}
public void On$$anonymous$$ouseExit()
{
transform.localScale += new Vector3(0F, 0, 0);
}
}
Please tell me what I did wrong: I'm quite new to game engines.
On$$anonymous$$ouseExist() = On$$anonymous$$ouseExit()
this script works fine for me - tested
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class $$anonymous$$ouseOver : $$anonymous$$onoBehaviour
{
public void On$$anonymous$$ouseEnter()
{
Debug.Log("enter");
transform.localScale += new Vector3(1.1F, 1.1f, 1.1f); //adjust these values as you see fit
}
public void On$$anonymous$$ouseExit()
{
Debug.Log("left");
transform.localScale = new Vector3(1, 1, 1); // assu$$anonymous$$g you want it to return to its original size when your mouse leaves it.
}
}