Move an Object from Point A to relative Point B

I’ve been pulling my hair out trying to wrap my head around properly using Lerp and MoveTowards. I’ve been able to program some relatively complex stuff in Unity, but I can’t get an object to translate from one point to another. I don’t think the Unity Reference on this subject is very clear (I’ve read it several times and tried the examples).

Here’s what I’m trying to do.

I have a series of blocks in the world. When I mouse over one I want it to raise up 2 units, and when I move the mouse off of it, I want it to drop back to where it was. The trick is, I need this movement to be relative to where the object was in the first place. Everything I’ve tried so far is an absolute movement.

This is the closest I’ve been able to get:

var speed = 45.0;
function OnMouseOver () {

var height : float = Mathf.MoveTowards (transform.localPosition.y, transform.localPosition.y+0.2, speed * Time.deltaTime);
transform.localPosition = Vector3(transform.localPosition.x, height, transform.localPosition.z);


This will make the object rise up, but it will continue to rise if you hold the mouse over it. It also seems like a lot of code for such a simple movement. Can anyone help make sense of this for me please?


Use a coroutine, such as MoveObject, which has an option for relative movement. Then it’s just one line of code. (Make sure you only call it once though, such as in OnMouseEnter rather than OnMouseOver.)