Checking for mouse input over a specific game object.

Well, logically I can think of how to check whether your mouse is over top of a mesh or other game object (cube for example). I know I could just use checks for where the x and y corrdinates are of the mouse, and then compare it to the x and y coords of the object, then check to see if they are equal. What I am wondering is, does the Unity API have something to do this already? What I want todo is move an object if it is clicked on then dragged. So does unity provide some function from the API?

Thanks,
Jedd

edit - Just realized this should be in the Scripting support forum, can a Mod please move this? Thanks

Add a script to your game object that has the OnMouseDown() function. Take a look at the Procedural Example from the Unity web site and look at the DragTransform.js script.

Ahhh, thanks! I know what to do now :slight_smile:

Thanks :smile:

Hmm, I have a blender mesh. I can move it fine, but I try to highlight it, it doesn;t work.

var highLightColor = Color.yellow;
var isSelected = false;

function MouseClickSelect() {
	if( Input.GetMouseButtonDown(0) ) {
		isSelected = true;
		renderer.material.color = highLightColor;
	}
}

I try the example script I found on the main page, but that doesn’t work also. Is there something about .blender files, or am I missing something?

You either need to name your function OnMouseDown() or call MouseClickSelect() from inside OnMouseDown().

It still doesn’t work.

var highLightColor = Color.yellow;
var isSelected = false;

function MouseClickSelect() {
	if( Input.GetMouseButtonDown(0) ) {
		isSelected = true;
		renderer.material.color = highLightColor;
	}
}

function OnMouseDown() {
	MouseClickSelect();
}

It must be something todo with the file type…

Does your object have a collider? Mouse picking operates on colliders; for imported meshes check “meshes have colliders” in import settings.

No it doesn’t. I don’t see where the import setting are. Also, I just tried adding a mesh collider to it by highlighting the mesh then going to Componet-> Physics → Mesh Collider. But it still doesn’t work :?

edit - I forgot to mention, when I add the script to it, a window pops up saying I will Lose my Prefab if i continue. I’m not sure what Prefab is, sooo…

Why are you doing:

if( Input.GetMouseButtonDown(0) ) { inside of OnMouseDown?

Just remove that part.

Also make sure the game object to which you are adding the script mesh collider is the same and actually contains the mesh renderer, and is not eg. just a node containing some children.

I did that because yoggy said in an above post… How can I tell the difference between just a node and an actual mesh renderer? I added a mesh renderer to it, if that is what you mean.

I have this code now

function OnMouseDown() {
	isSelected = true;
	renderer.material.color = highLightColor;
}

and still no go. :frowning:

I think the problem is not your code. You need to have both a mesh renderer and mesh collider in your game object. The mesh renderer only controls what is displayed onscreen; the collider is what actually determines whether the mouse is over the object or not.

All right, I now know that it is reacting, the isSelected variable turns on (true) when I click on the mesh. Just it doesn’t turn red…

Do you have a renderer on the same object? What shader is it using? Does the material’s color turn red in the inspector? Any errors in the message bar?

Yes, a mesh renderer/

Diffuse.

Yes ( I think ). A new bar under mesh renderer appears saying “Default-Diffsue Instance (Material)
Shader: Diffuse
Main Color [shows red]
Base (RGB) [Box that says None]”

Nope…

Then everything Should Just Work. Can you send in the project folder with Report Bug.app, maybe there’s something really funky happening?

Yeah I’ll upload it in a little while. Right now I’ve got to sign off, so when I have time tonight I will. Should I delete anything to save space, since it’s 50 mb?

50MB is fine with us.

All right, I’m sending now :slight_smile:

Oh, I should mention, when I upgraded to the Pro Version, then tried to import a file (the .blender) by: Asset->Import New Asset I recieved this error:

Should I report this as well? Seems to be an error in the actual source, that reports itself.

Do you have a collider on your camera? I found the mouseover functions do not work if there is a collider of sufficient size on the camera, blocking the detection since it’s basically ‘trapped’ inside another collision body.

Setting ‘Is Trigger’ to ON doesn’t change this.

Example, a camera with a sphere collider with 0.5 radius seems to ‘block’ the mouseover. Changing the radius to .05 doesn’t block it.