super noob translate doubt

Super Noob Question

There is a box which, on mouse down should rise by 25 units. (I am able to do this by the transform.translate method)
But upon mouse click again, the box should go back and sit on its original position.

(I’ve spent an entire week going through forums/ tutorials, but in vain)

I dont know how to set variables (if this is what it requires) :sweat_smile:

PLEASE HELP !!!

Hi,

There are a couple of ways to do what you’ve asked:

The most obvious would be to add a override function OnMouseUp() (see Unity scripting reference - OnMouseUp) and use the transform.Translate(-25) since you’ve only moved in a single direction.

You could, alternatively, record the starting transform (including position and rotation) and reset the values OnMouseUp.

These are 2 methods, there are others.

Hope this helps,

Galen

Another approach would be to check the y-position*, if it’s at the lower value then raise it, if it’s at the higher value then lower it.

*I’m assuming when you make the object “rise” that it’s doing that in the y-direction.

Quick, untested forum code:

if (MyGO.transform.localPosition.y == 0.0) {
  // raise the object
  MyGO.transform.Translate(0, 25.0, 0);
} else if (MyGO.transform.localPosition.y == 25.0) {
  // lower the object
  MyGO.transform.Translate(0, -25.0, 0);
}

this is what I have added to the cube

function OnMouseDown ()
if (MyGO.transform.localPosition.y == 0.0) {
// raise the object
MyGO.transform.Translate(0, 25.0, 0);
} else if (MyGO.transform.localPosition.y == 25.0) {
// lower the object
MyGO.transform.Translate(0, -25.0, 0);
}

attached are the errors :frowning:

  1. Unexpected token: if
  2. ‘;’ expected. Insert a semicolon at the end.
  3. expecting :, found ‘;’

Still not working !!! :cry: :cry:

I think I got paranoid and hit the submit button four times !!! :?

Try this:

function OnMouseDown () {
   if (MyGO.transform.localPosition.y == 0.0) { 
      // raise the object 
      MyGO.transform.Translate(0, 25.0, 0); 
    } else  { 
      // lower the object 
      MyGO.transform.Translate(0, -25.0, 0);
    }
}

:shock:

error is …
Unknown identifier: ‘MyGO’

To think of it, I have to give a demo to a client tonight :cry:
I am sooooooo dead !!!

If this script is attached to the game object in question then replace MyGO with “gameObject” and go from there (I don’t want to write your script for you, I’m trying to lead you to the well and make you drink on your own :slight_smile: ). I hope your demo went well or got rescheduled…

It sounds to me like you’re just starting with scripting and so I really encourage you to work through the tutorials on our site and the community wiki before going too much further. That way you’ll develop a solid foundation first as we’re explaining what should be ultra-simple things for scripters to sort out. We all start somewhere so I’m not knocking you, I’m just saying that basic intro tutorials is where you ought to go next.

Unity Resources
Wiki Tutorials

:lol:
Couldnt agree more.
I think I should stick to the art part of it and leave the scripting to the pros.
Got a friend to help me out with it.
Thanks for your attention.