- Home /
OnMouseOver how does it work?
Hi im trying to make it so when i right click a cube it loads the game scene but i cant get it to work can you guys help me out? ive also tried onmousedown but not matter where i click it loads the scene
function OnMouseOver()
{
if(Input.GetMouseButtonDown(1))
{
Application.LoadLevel ("GameScene");
}
}
Your script worked fine for me. $$anonymous$$ake sure the object has an enabled collider (should if it is a built-in cube). Put Debug.Log() statements in the code to see what is happening.
Debug.Log("box is being clicked"); i put a debug.log but it shows nothing
Answer by chris_taylor · Feb 28, 2014 at 10:45 PM
OnMouseOver will happen anytime the mouse goes over a collider and it will call the onmouseover on that collider. If you want it to be a click do OnMouseDown for a right click you will need to test what click happened so use Input.GetMouseButtonDown(1) inside the onmouseDown function or even on onmouseover
ok i tried this but still when i rigt click it does nothing function On$$anonymous$$ouseDown() { if (Input.Get$$anonymous$$ouseButtonDown(1)) { Application.LoadLevel ("GameScene"); } }
i do a box collider does it need to have is trigger checked? i aslo ahvea few bouncy scripts on it to work with my water
ok i got it working by calling the onmosue down from the update funtioon but now no matter where i click it loads the scene
On$$anonymous$$ouseDown does not get called in the Update, it is an event method that is called automatically. In order to get the call, try this setup, place a cube, it should have the collider by default. Then attach that script:
public class On$$anonymous$$ouseDownTrial:$$anonymous$$onoBehaviour{
void On$$anonymous$$ouseDown(){
print("Working?")
}
}
Now run the game and click on top of the cube, you should see the print.
To fully understand, place another cube, name it Cube2 and attach the script like this:
public class On$$anonymous$$ouseDownTrial:$$anonymous$$onoBehaviour{
void On$$anonymous$$ouseDown(){
print("Clicking on" + name)
}
}
Now you see that the script prints different names. If you need to have different behaviours then create different scripts with the On$$anonymous$$ouseDown implemented differently.
You can also check for Input to see what button was pressed.
The other way consists in using Input and Raycast, it will result in the same thing but more code and maybe more control as well.
public RayAndInputTrial:$$anonymous$$onoBehaviour{
void Update(){
if(Input.Get$$anonymous$$ouseButtonDown(1)){
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 100)){
if(hit.collider.name = "Cube")print("Hit cube")
}
}
}
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Help In Making a SphereCast for 3D Tire! Working RayCast Script included! 0 Answers
UnityEngine.Input.GetMouseButton(1)) issue 1 Answer
I made a better shader how do i fix[add _Shadow Strength]help???>Sorry that im asking for to much 1 Answer
scripting help 2 Answers