I have a problem, I need my character to move a certain distance when I click the button, until then I can do it.
The problem is that when I do, he does not "walk" to the point, he kind of "teleports", and that's not the intention. I need him to automatically walk this distance.
Can anyone 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;
}
}
}