lerp to a specific x,y,z coordinate

Good morning, all.

I have a code whereby my prefab lerps to a game object in the scene. It works fine and is actually a small portion of a larger code that is working. But I think for purposes of my program it would be better if I could set some variables and have it move to the variable coordinates.

Here’s the portion that moves my prefab to the other game object:

var liftSpeed: float = 10;
var target : GameObject;


function Update() {

	
		transform.position = Vector3.Lerp(transform.position, (target.transform.position), Time.deltaTime *      liftSpeed);
	}

Now what I would like to do, but don’t know how, is to remove the dependency on the “var target : GameObject;” and set actual variables for x,y,z coordinates. Or even more simple, to just have the prefab move to a specific y coordinate. If anyone has any suggestions or direction I would greatly appreciate it.

Thanks and best regards
-Richard

var liftSpeed: float = 10;
var targetPosition : Vector3;


function Update()
{
    transform.position = Vector3.Lerp(transform.position, targetPosition, Time.deltaTime * liftSpeed);
}

Done! Set ur target position in the editor there.

–TwisterK

Awesome! So its of type “Vector3”. Thank you SOOOO much. That actually helps me out with about 10 other code snippets I have been working on.

Best regards
-Richard

Ok.

So know I’m trying to change it up using OnMouseOver. But I am trying to get the object to return to its start position. I may be using the wrong command here to get it back.

So, the object should be moving toward the camera when the mouse is over, and back to where it began when it isn’t, right?

The problem is that OnMouseOver/OnMouseExit are only called once - so it’s only moving the object once, thus it doesn’t have enough time to return to the position. What I’d do is set a flag when the mouse is over, and lerp to the start or end position based on that flag, like so:

var liftSpeed: float = 2;
var targetPosition : Vector3;
var startPosition : Vector3;

private var _mouseOver = false;
function OnMouseOver () {
 _mouseOver = true;
}

function OnMouseExit () {
 _mouseOver = false;
}

function Update(){
 var wantedPosition = _mouseOver ? targetPosition : startPosition;
 transform.position = Vector3.Lerp(transform.position, wantedPosition, Time.deltaTime * liftSpeed);
}

Standard typed-in-browser-may-not-execute disclaimer applies. :wink:

Hope that helps.

Regards,

-Lincoln Green