- Home /
UI window preventing OnMouseOver event on 3D Object
I have been working on this for a while now trying both raycast and OnMouseOver and i think i have missed a step somewhere along the way
I have a 3D model that is set to rotate. I am trying to say when the mouse is over the object (or a mesh collider) then stop rotating. However nothing i have tried in the last 3 hours has worked at all.
on my 3D model i have a mesh colider and a c# script called "rotate.cs"
using UnityEngine;
using System.Collections;
public class Rotate : MonoBehaviour {
//private button = male;
public bool rotate = true;
void Start() {
}
void Update() {
transform.position = new Vector3 (500, 0, 0);
if (rotate == true) {
transform.RotateAround (Vector3.one, Vector3.up, 20 * Time.deltaTime);
} else {
transform.RotateAround (Vector3.one, Vector3.up, 0);
}
}
void OnMouseOver() {
Debug.Log("Mouse Over");
rotate = false;
}
void OnMouseExit() {
Debug.Log("MouseOff");
rotate = true;
}
}
Can someone go through step by step on how to get this working with either raycast or OnMouseOver event systems.
Answer by CHPedersen · Mar 20, 2015 at 09:05 AM
I just tested your script on a standard cube in an empty scene, and it behaves really odd. The cube rotates slowly but flickers violently as the call which sets transform.position fights the rotations.
The OnMouseOver and OnMouseExit methods execute perfectly fine. I did a minor change to your script, so now it looks like this:
using UnityEngine;
using System.Collections;
public class Rotate : MonoBehaviour
{
//private button = male;
public bool rotate = true;
void Start()
{
}
void Update()
{
if (rotate == true)
{
transform.Rotate(Vector3.one, 20 * Time.deltaTime, Space.Self);
}
}
void OnMouseOver()
{
Debug.Log("Mouse Over");
rotate = false;
}
void OnMouseExit()
{
Debug.Log("MouseOff");
rotate = true;
}
}
Try that. It removes the flickering and behaves the way you describe. You can use that as a starting point to move forward, at least.
Did you add anything else to the default cube, like colliders or other components ? i just created the cube and tried again with no luck. Also i have to use RotateAround with Vector3.up so it rotates around the up/down axis.
Nothing. Started a new scene, then GameObject->3D Object->Cube. Then dragged above script onto that. I positioned the camera a little to look at the cube better, but that's it. When I start, the cube rotates slowly, unless the mouse hovers over it. The rotation starts/stops on mouse over.
But... I'm running Unity 4.6. $$anonymous$$aybe something got changed for Unity 5. Are you testing this on Unity 5?
yes its unity 5 i just started using it for work ill try it on a new scene,. it could be the UI stopping the mouse over events.
And thanks for the Help, really appreciate it
Ok just tested it, It is the UI getting in the way. I have a layout.cs which creates a UI window and loades a logout button and a inputTextFiele.
I believe the mouse is interacting with the window and not the cube. is there a simple way of getting the cube infront of the window so the mouse interacts with it ins$$anonymous$$d ?
using UnityEngine;
using System.Collections;
public class layout : $$anonymous$$onoBehaviour {
private Rect windowsRect = new Rect(0, 0, Screen.width, Screen.height);
private string inputeString = string.Empty;
private static int inputwidth = Screen.width/6;
//private static int inputheight = inputwidth/3;
private static int textsize = 0;
private static int buttonHeight = Screen.height/24;
private static int buttonWidth = Screen.width/12;
void start() {
if (Screen.height/15 <= inputwidth/4) {
textsize = Screen.height/15 ;
} else {
textsize = inputwidth;
}
}
void OnGUI()
{
GUI.Window (0, windowsRect, WindowFunction, "$$anonymous$$edic AL");
GUI.skin.textField.fontSize = textsize;
}
void WindowFunction (int WindowID) {
inputeString = GUI.TextField (new Rect (Screen.width - Screen.width / 3, 2 * Screen.height / 5, Screen.width / 3, Screen.height / 10), inputeString);
if (GUI.Button (new Rect (buttonHeight/2, buttonWidth/4, buttonWidth, buttonHeight), "Logout")) {
Application.LoadLevel ("StartGUI");
}
}
void Update () {
}
}
Good find.
I'm not sure how to solve that one as my work hasn't invested in Unity 5 yet, so I have zero experience playing around with the new UI system, sadly. :/ But it looks to me like their UI mouse features are based on the same raycasting framework as the standard On$$anonymous$$ouseExit/Over etc. methods used with GameObjects that have colliders. If that is the case, then UI elements too must have colliders. Perhaps those can be disabled? Or perhaps there's a Layering system that allows you to ignore the UI layer?
Your answer