What's the best way to move to a target?

I have two objects.

Object 1 and Object 2.

I need to when I keydown “space”, my object 1 go to object 2 position.

I tryied using :
transform.position = Vector3.MoveTowards(transform.position, target.position, step);

But it don’t work like “Translate”, so I need to seed it moving. Using subtraction is possible, but have a best way to do it?

Thanks,

Sorry bad english, i’m not a english speaker.

function Update () {

if(Input.GetKeyDown(KeyCode.Space)) {

what I put here?


}

This will move the object towards it’s target at 1 unit per tick.

public Transform target;
public float step = 1.0f;
function Update () {
	if(Input.GetKey(KeyCode.Space)) {
		transform.Translate(Vector3.Normalize(target.position-transform.position) * step); 
	}
}

Alternatively, if you want a nice smooth movement, you can use Linear Interpolation. Something like:

public Transform target;
function Update () {
	if(Input.GetKey(KeyCode.Space)) {
		transform.position = Vector3.Lerp (transform.position, target.position, 0.1f);
	}
}

This will move the object 10% closer to it’s target every tick.

2 Likes

By the way, thanks cowtrix for this. I was going to have to look up how to use Lerp again tonight, but you just saved me a ton of effort. Thanks!

Edit; Actually, a bit of a small mistake: you can only send one float into Lerp, while transform.position is three, as in Vector3.

So… [quick JS example]

transform.position = Vector3(Mathf.Lerp (transform.position.x, target.position.x, 0.1f),0,0);

Not sure if you can Lerp a Vector3 all at once, but in C# you can’t… So maybe I’m wrong, and this DOES work originally in JS with Vector3’s. Can anyone confirm?

Ah, my bad. You can indeed lerp a Vector3 in one line. In my example I used Mathf.Lerp, not Vector3.Lerp. I updated my example.

Thanks for the clarification. Also, found this:

It helps for smooth lerp effects.

Thanks a lot cowtrix and MDragon.

It goes just towards the object 2. But in this case I have to keydown “space” repeatedly.

I need to see it going there when keydown “space” once e see it’s moving.

If you use that link I posted above, it should solve your problems. :wink: If you need to convert it to C#, feel free to ask, because I had to do that as well.

Using Lerp, It goes toward the object 2, but when I use KeyDown space, don’t works. I tryied to use ‘while’, like the link, but I don’t know what the parameter “time” means…

Time is just that: time in “seconds” that you pass into the function. So, if we were using C# (if you use yield, you have to call it using CoRoutine and make it an IEnumerator type in C#):
(I say “seconds” because, as far as I could tell, when I put in 2 seconds, it took longer than 2 seconds)

    public void BackPos(){
        if(currentPos != 0){
            currentPos--;
            StartCoroutine(MoveCam(this.transform, transform.position, targets[currentPos].position, 0.5f));
            if (currentPos == 0)
                panLeft.GetComponent<Control_Texture>().ToggleDestination(false);
        }
        panRight.GetComponent<Control_Texture>().ToggleDestination(true);
    }

    IEnumerator MoveCam(Transform thisTransform, Vector3 startPos, Vector3 endPos, float time){
        float i = 0.0f;
        float rate = 1.0f/time;
        while (i < 1.0){
            yield return new WaitForEndOfFrame();
            i = i + (Time.deltaTime * rate);
            thisTransform.position = Vector3.Lerp(startPos, endPos, i);
        }
    }

That’s straight from my code. Notice how I called the function: I gave it all the parameters in that order… and sorry for all the other irrelevant information in this example. :wink:

Hey, MDragon!

I got it using the code in the link.
I’d like to thank you for everything!


Edit: But…after I will need to “follow” the target moving only the x and z. What’s the best way? Is possible use a Vector that storages only x and z?

Everyone is suggesting Lerp but neglecting the fact that they’re using it to say “move from A to B in C seconds” which might not be what the OP wants. If you want “move from A towards B at C speed” then you’d want something like this (which is also much simpler)

void Update()
{
    if (Input.GetKey(KeyCode.Space))
    {
        transform.position += (transform.position - target.position).normalized * speed * Time.deltaTime;
    }
}

Note that this code will also work to “follow” the target if it moves.