- Home /
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?
Thanks.
Answer by Eric5h5 · May 25, 2012 at 04:01 AM
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.)
Your answer
Follow this Question
Related Questions
Leading Reticle 0 Answers
How do I translate around a circle? 3 Answers
Smooth swapping my game object 0 Answers
3 coroutines to ease in lerp, MoveToward steady pace, and ease out lerp 0 Answers