- Home /
how to detect mouse click on a gameobject.
I have an assignment and I am lacking idea in this regard. I am surely a new bee and eagerly waiting for the help.
http://www.gamefools.com/onlinegames/free/supercollapsepuzzlegallery2.html
Please see the game
By looking at this game I got two ideas, one is to use cubes(with rigidbody and colliders attached) and other is to use the GuiTexture. If using cubes, I am think that how to detect the mouse click on individual cube and if using GuiTexture than how to know whether something is below or not.
Please help me
Answer by Pysassin · Jan 03, 2013 at 09:56 PM
As far as the cube method goes you can use a simple mouse over function and ignore the raycasting entirely. look something like...
function OnMouseOver(){
if(Input.GetMouseDown(0){
// Whatever you want it to do.
}
}
That is for JS though the method could easily be ported to C# as well.
Pysassin thanks.... your code really worked....
But one thing is that its not Input."Get$$anonymous$$ouseDown" but Input."Get$$anonymous$$ouseButtonDown"
Anyway lots of thanks
u forgot ")" after "if(Input.Get$$anonymous$$ouseDown(0)" so it should be ... if(Input.Get$$anonymous$$ouseDown(0) ) ... thanks
Hey how do u make it to where it only works on a certain tag?
I had the same question, and this worked for me. Here's the C# version: void OnMouseOver() { if (Input.GetMouseButtonDown(0)){ //whatever you want to do here } }
Answer by sampathcse16 · Oct 29, 2019 at 07:47 PM
You can use the below script to identify the mouse click on particular game object.
void Update()
{
//Check for mouse click
if (Input.GetMouseButtonDown(0))
{
RaycastHit raycastHit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out raycastHit, 100f))
{
if (raycastHit.transform != null)
{
//Our custom method.
CurrentClickedGameObject(raycastHit.transform.gameObject);
}
}
}
}
public void CurrentClickedGameObject(GameObject gameObject)
{
if(gameObject.tag=="something")
{
}
}
Answer by Maulik2208 · Jan 04, 2013 at 08:41 AM
void OnMouseUp()
{
/*Do whatever here as per your need*/
}
As well as you can use
void OnMouseDown()
{
/*Do your stuff here*/
}
USe this link to get more info regarding mouse related function---->link text
If found useful then don't forget to mark the answer.......
It requires Collider to be added to Game Object beforehand
Answer by shigidaMark · Jan 03, 2013 at 06:53 PM
This will check each frame if there is something under the mouse. Then it checks if it is a cube (via tag). If so you could manipulate the cube using hit.collider.gameObject (such as changing it's forward/backward position, size, material, etc.):
function Update(){
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
var hit : RaycastHit;
if(Physics.Raycast(ray,hit)){
if(hit.collider.tag == "clickableCube"{
//hit.collider.gameObject now refers to the
//cube under the mouse cursor if present
}
}
}
As for the GUITexture, you can just use that in the OnGUI function to replace the mouse cursor:
var crosshair : Texture;
function Start(){
Screen.showCursor = false;
}
function OnGUI(){
var pos = Input.mousePosition;
GUI.DrawTexture(Rect(pos.x-crosshair.width/2,pos.y-crosshair.height/2,crosshair.width,crosshair.height),crosshair);
}
Can you show me how can I make this game using only GuiTexture? Please shigda$$anonymous$$ark.... and lot of thanks Sir
I am getting this error message for hit variable...
Use of unassigned local variable `hit'
Answer by lemonyama · Jul 29, 2015 at 03:36 PM
Updated script for this which should work okay:
if (Input.GetMouseButtonDown (0)) {
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 100)) {
// whatever tag you are looking for on your game object
if(hit.collider.tag == "Trigger") {
Debug.Log("---> Hit: ");
}
}
}
Use " var ray = Camera.main.ScreenPointToRay(Input.mousePosition);" ins$$anonymous$$d of " var ray = Camera.current.ScreenPointToRay(Input.mousePosition);" I got an error with the second line of code but using "main" got rid of the error.
Your answer
![](https://koobas.hobune.stream/wayback/20220613090333im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to make a mouse click selection for in game use 1 Answer
How to do a Burst Particle effect on Mouse Click? 2 Answers
Unity2D problem with negative z rotation shooting bullet towards mouse click 1 Answer
How to different hold and one click left button mouse? 1 Answer
Using "Vector3.MoveTowards" to translate an object to mouse click 1 Answer