- Home /
How to move an object from point A to point B with one key press
I'm sure this must be amazingly simple but I can't figure how to move one object from one point to another by using a single keystroke.
At the moment I'm trying get a platform object to smoothly lift from point A to point B when I press the spacebar.
My current code looks like this:
var target : GameObject;
private var liftSpeed = 10;
function Update () {
if(Input.GetButtonDown("Jump"))
{
transform.position = Vector3.Lerp(transform.position, target.transform.position, Time.deltaTime * liftSpeed)
}
}
This however, only moves the platform by the specified fraction, and I have to repeatedly press space until the platform reaches its target. How do I get it to continually move upwards until it reaches the target by just pressing the spacebar once?
Thanks :)
Answer by skovacs1 · Sep 15, 2010 at 02:27 PM
Your code is only running when you hit the space bar because it will only perform the transformation inside the if statement when the space bar is hit.
If you wanted it when you hold the space bar, you would use Input.GetButton("Jump")
instead.
If you want it just to start the transform and run on its own, you could set a flag or something like this:
var target : GameObject; //destination var liftSpeed : float = 10; //speed (it will complete the motion in 1/speed seconds) private var moving : boolean = false; //flag private var weight : float = 0; //amount moved private var startPosition : Vector3; //Where we start;
function Update () { if(target) { if(Input.GetButtonDown("Jump")) { //just pressed Jump startPosition = transform.position; //Set the start moving = true; //set flag } if(transform.position != target.transform.position) moving = false; //reset flag if(moving) //check flag weight += Time.deltaTime * liftSpeed; //amount transform.position = Vector3.Lerp(startPosition, target.transform.position, weight) } } }
The reason I changed the code to store a start position and weight is to provide a linear interpolation. By lerping from the current position (which you are constantly changing) and a target, you will move by the weight amount of the remaining distance (which is constantly decreasing), meaning that the movement will start fast and will slow down as it approaches the destination which it may never actually reach. There's nothing wrong with this form of interpolation and if that's what you wanted, by all means change that part back. If you wanted it to smooth in and out, I'd recommend using something like Mathf.SmoothStep(weight) in the Lerp.
Answer by cengizhangokben · May 28, 2013 at 08:00 AM
was not there an error? It must be the condition like that;
if(transform.position == target.transform.position)
devam = false; //reset flag
:)
Answer by bbyford · Jan 31, 2015 at 11:19 PM
Quick moving an object script via transform in C# with timetaken:
bool somethingMoved
somethingMoved = moveSomething(yourObject, endObject); // change room bool
// move GameObject to GameObject marker
float timeTaken;
public bool moveSomething(GameObject start, GameObject end){ // return bool when finished moving
if(start.transform.position == end.transform.position){
return false;
}else{
timeTaken += (float) move(start, end);
return true;
}
}
public float move(GameObject start, GameObject end){
start.transform.position = Vector3.Lerp(start.transform.position, end.transform.position, Time.deltaTime*camSpeed);
return Time.deltaTime*camSpeed;
}