- Home /
How to MouseOver Button on a Mesh?
I really need help with this. I want to make a Main Menu where a Locker and a Laptop is shown. If I go to the Locker with the Mouse the Locker should play my opening animation and if the cursor is at the Laptop, the Screen should go on. I tried it with a nondrawed cube mesh in front of the Locjer to detect the mouseOver but i'm stucked because of my "great" coding skills.
Thanks.
Answer by Gr33nIguana · May 25, 2013 at 08:49 PM
You can give your laptop 3d model a collider (components -> Physics -> any collider) Then make a new c# script, name the script as the name of the public class in the script (in this case AnyName) copy paste the code below, save and put this script on your 3d model of the laptop. Don't forget to fill the variable of the script ;-)
using UnityEngine;
using System.Collections;
public class AnyName: MonoBehaviour {
public GameObject theLaptop;
public GameObject theLocker;
void OnMouseEnter() //detects if a mouse is on the object
{
theLaptop.animation.Play("name of animation here"); //use this to play an animation file
theLaptop.renderer.material.color = Color.red; //use this to change the color of the material
}
void OnMouseExit() //detects if a mouse is going off the object
{
theLaptop.renderer.material.color = Color.white; //use this to change the color of the material
}
void OnMouseDown() //detects if a mouse button is pressed
{
theLaptop.renderer.material.color = Color.yellow; //use this to change the color of the material
}
}