OnMouseDown Help

Hey everyone.

I am currently working on a scrip that when you click the mouse button, the object moves in the “y” direction so many units. I have that working.

But the part I do not understand is if I click the mouse again, I want the object to return to it’s original location.

Here is what I have:

function OnMouseDown () 
{
	transform.position = Vector3 (107,0,66);
		
}

I tried a few things but to no avail.

Any help would be appreciated.

Thanks,

David

function OnMouseDown()
{
   if(transform.position == (Vector3 (0,0,0)))
   transform.position = Vector3 (107,0,66);
   else
   transform.position = Vector3 (0,0,0);
}

function Update()
{
	if(Input.GetMouseButtonDown(0))
	{
		OnMouseDown();
	}
}

easy fix is to add a bool to it.

hasClicked = false;

function OnMouseDown () 
{ 
   if (hasClicked)
   {
      hasClicked = false;
      //reverse direction here
   } else {
      hasClicked = true;
      //move this direction
   }
}

Now, with transform.position you’re actually “teleporting” the character in that direction. I’m unsure if this is the effect you wanted, but I just thought I’d make you aware.

Regards,

Thank you for the responses.

I will work with both examples and learn from then. I know I had to use an if statement, but being new at this I really wasn’t sure about it.

And yes I know I would be teleporting instead of an animated move. I am just learning as much as possible with all different scenarios. I will work on an animated move next.

Thanks again for the information it will be useful during the learning process.

David

**** EDIT:

Ok well I looked up the Input.GetMouseButtonDown in the scripting reference and it says Left is 0, 1 is right and 2 for middle.

But I changed the value to one and I am Left clicking on the object and it works, but if I right click in open space it also works on the object. Now I am confused even more???

I want the command confined to clicking on the object only.

appels,

I tried you version first and it works great. I had to change the value after Input.GetMouseButtonDown (0) to 1.

If the value was on 0 I had to click outside the game object I wanted it to affect. If I changed it to 1 then I had to click the game object to get it to work.

Could you please explain that last part of the script and what was the difference between 1 and 0. I am assuming true or false but I am not sure.

Thanks again,

David

Ok I figured it out.

The last part of the code is not needed. Once I removed it, the code worked exactly as expected.

Final Code was :

function OnMouseDown () 
{
	if (transform.position == ( Vector3 ( 107, 2.5, 66 )))
	transform.position = Vector3 (107,0,66);
	else
	transform.position = Vector3 ( 107, 2.5, 66 );
}

Ok here is how I translate this code.

You are stating the OnMouseDown, if the objects current position is equal to (107,2.5,66)
When you click change it to this (107,0,66)
else ( meaning that if it is not at (107,2.5,66) then move it to (107,2.5,66)

I hope I am reading this right.

Thanks again,

David