Basic On Off movement using mouse click

Hi Guys, I've modelled a car engine and I'm after a script so if I click say, the cylinder head it will move about 2 foot upwards. Then if I click the cylinder head again it will move back to its original position.

I'm a bit of a noob at coding but can just about get by at it. I understand I need a boolean,and a raycast for the mouse.

Would I need two scripts with one turned off? One script to move the object and switch itself off, and switch another script on which will move the object back to its origin.

Any help would be greatly appreciated.

Cheers

Richard

You don't need two scripts, and you don't need a raycast. Something like this should do it:

var raiseDistance : float = 0.5;
private var originalPosition : Vector3; 
private var isRaisedPosition : boolean = false;

function Start() {
    // store original position
    originalPosition = transform.position;
}

function OnMouseUp() {

    // toggle value which determines which position we're using
    isRaisedPosition = !isRaisedPosition;

    // apply correct position
    if (isRaisedPosition)
    {
        transform.position = originalPosition + transform.up * raiseDistance;
    } else {
        transform.position = originalPosition;
    }
}

The script above raises and lowers the object along the object's local up axis. There are a number of ways this script could be made more flexible if necessary (such as moving the object some amount in an arbitrary direction, and perhaps a toggle to indicate whether the movement should be in a world-relative or object-relative direction). Hopefully this is enough to get you started though.