Move character to the point in Unity3d

Asked

Viewed 1,831 times

3

I have a problem, I need my character to move a certain distance when I click the button, until then I can do.

The problem is that when I do, he doesn’t "walk" to the point, he sort of "teleports," and that’s not the intention. I need him to automatically travel this distance.

Can someone help me?

using Unityengine; using System.Collections;

public class movement : Monobehaviour {

public float posicao = 2f;
private bool clickBaixo;
private bool clickCima;

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {

    clickBaixo = moveDown.getBaixo();
    clickCima = moveUp.getCima();

    if(Input.GetKeyDown("up") || clickCima == true){
        transform.Translate(0,posicao,0);
    }

    if(Input.GetKeyDown("down") || clickBaixo == true){
        transform.Translate(0,(posicao)*-1,0);
    }
}

}

Updated:

using Unityengine; using System.Collections;

public class movement : Monobehaviour {

public float posicao = 2f;
public float vel = 10f;
private bool clickBaixo;
private bool clickCima;
private float novaPosicao;

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {

    clickBaixo = moveDown.getBaixo();
    clickCima = moveUp.getCima();

    if(Input.GetKeyDown("up") || clickCima == true){
        StartCoroutine("moveCima");
    }

    if(Input.GetKeyDown("down") || clickBaixo == true){
        StartCoroutine("moveBaixo");
    }
}

IEnumerator moveCima(){
    novaPosicao = transform.position.y + posicao;
    while(transform.position.y<novaPosicao){
        transform.Translate(0, vel*Time.deltaTime, 0);
        yield return null;
    }
}

IEnumerator moveBaixo(){
    novaPosicao = transform.position.y + posicao;
    while(transform.position.y<novaPosicao){
        transform.Translate(0, (vel*Time.deltaTime)*-1, 0);
        yield return null;
    }
}

}

  • 1

    Could you edit the question to post the relevant part of your code on it? After all, you can’t tell what’s not working without seeing it.

  • 2

    As colleague @Victorstafusa mentions, your code is important so someone can point out your error. You are probably changing the transform.position from it to the destination position (final), and so occurs the "teleport". If applicable, you should use an alternative such as (1) using physics and applying a force in the direction of fate; (2) point the character’s object in the direction of destiny and gradually translate according to a speed stipulated in that direction every frame (call for update); (3) use Lerp.

1 answer

2


You need to know what effect you want when walking with your character because there are several solutions. And they all depend on what you want to do. The easiest ones are.

The first is to use a force, you apply a force to rigidybody and Unity takes charge of changing the position for you. https://youtu.be/PmA0paVG16g

The second is you add up small steps by updating the position to get from one point to the other. Do not use while inside an Update, try using a Coroutine. In this one I use the MoveTowards https://www.youtube.com/watch?v=uTFfna91u4g

The third is to use the Lerp, just like Movetoward, remember to use a time variation to ease the transition https://youtu.be/uGoAqv-uy6E?t=11m25s

There is also to be careful where you put your loopings and what you put where. For there is a big difference in the calls of Update and of FixedUpdate. https://youtu.be/0rD4OuPxsuk

  • I tried to walk to, few with a while but got a giant lag, if you can give me an example of this case, put strength in rigidybody did not try, I will try when I can. Thank you for your answers.

  • Stay away from the while to do these things if you’re not wearing a Coroutine. I’ll edit the answer, but again, to be more direct, I need to know the effect it’s looking for on your character.

  • thank you very much, I’ll try these ways, then I’ll tell you what I got...

  • Thank you very much Nils, I managed to do with a courotine, I had never used the courotine before, I started programming games at 1 month maximum. follows updated question code.

  • Damn it, you did well to what you started to do now. Few people I know choose this solution precisely because it is more advanced. Congratulations!

  • 1

    I am web programmer, I have programming knowledge, but not in games, rs.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.