Noticias

¡RECUERDA QUE SI ERES UN NUEVO USUARIO, DEBES PRESENTARTE PARA PODER PUBLICAR MENSAJES! | TENEMOS CANAL OFICIAL DE TELEGRAM: t.me/unity3dspain

Correr apretando dos teclas

Iniciado por ramunda, Julio 14, 2016, 12:58:42 AM

Tema anterior - Siguiente tema
Buenas noches,
 
Una pregunta por favor:
 
Tengo un simple input de teclas que mueven mi personaje con una animación con tecla asignada.
 
Quiero hacer que al apretar la tecla "flecha arriba" + control derecha , pues en vez de andar corra.
 
Para eso he creado dos variables distintas:
 
Velocidad y Correr con sus respectivas animaciones.
 
Pero al apretar simultaneamente o una tras de otra , no se ejecuta , le he puesto un debug,log pero tampoco aparece.
 

using UnityEngine;
using System.Collections;
public class moverplayer : MonoBehaviour
{
// En esta variable configuraremos la velocidad a la que se moverá el objeto
public float Velocidad = 1.0F;
public float Correr = 4.0F;
private Animation anim;
public float jumpHight = 0.5F;
public float jumptime = 0.300F;
public float smoothFactor = 5;
//public Quaternion rotation = Quaternion.Euler(new Vector3(0, 30, 0));
 
 
void Start()
{
   anim = GetComponent<Animation> ();
}
 
void Update ()
{
   
   // Movemos el objeto hacia adelante
   if (Input.GetKey (KeyCode.UpArrow)) {
      transform.Translate (Vector3.forward * Time.deltaTime * Velocidad);
      transform.forward = new Vector3 (0f, 0f, 1f);
      anim.Play ("walk00");
   }
   else if(Input.GetKey(KeyCode.DownArrow))
      
   {
      //atras
      transform.Translate(Vector3.back * Time.deltaTime * Velocidad,Space.World);
      transform.forward = new Vector3(-0f,0f,-1f);
      anim.Play ("walk00");
   }
   else if(Input.GetKey(KeyCode.RightArrow))
   {
      //derecha
      transform.Translate(Vector3.right * Time.deltaTime * Velocidad,Space.World);
      transform.forward = new Vector3(1f,0f,0f);
      anim.Play ("walk00");
   }
   else if(Input.GetKey(KeyCode.LeftArrow))
   {
      //izquierda
      transform.Translate(Vector3.left * Time.deltaTime * Velocidad,Space.World);
      transform.forward = new Vector3(-1f,0f,0f);
      anim.Play ("walk00");
   }
   else if(Input.GetKey(KeyCode.UpArrow) && Input.GetKey(KeyCode.RightControl))
   {
      
      Debug.Log ("correee");
      transform.Translate (Vector3.forward * Time.deltaTime * Correr);
      transform.forward = new Vector3 (0f, 0f, 1f);
      anim.Play ("run00");
   }
   else
   {
   anim.Play ("idle01");
   }
      
}
}

 
Agradeceria un consejo .
 
Gracias
 
 
 
 
 
 
 
 

Porque se ejecuta en orden, cuando se actualiza el Update ejecuta línea a línea las sentencias, tienes puestos varios else if, en cuanto uno es verdadero los otros no los tiene en cuenta, pon primero la condición doble y luego las simples. Otro consejo es no mover los jugadores con Rigidbody usando el Transform, por las físicas.

Julio 14, 2016, 01:33:01 AM #2 Ultima modificación: Julio 14, 2016, 01:44:28 AM por ramunda
Cita de: juanma_teso date=1468451909Porque se ejecuta en orden, cuando se actualiza el Update ejecuta línea a línea las sentencias, tienes puestos varios else if, en cuanto uno es verdadero los otros no los tiene en cuenta, pon primero la condición doble y luego las simples. Otro consejo es no mover los jugadores con Rigidbody usando el Transform, por las físicas.
   


Correctísimo Juanma_teso, no había caído en lo de que se ejecutaban en orden...
 
Y lo de los else if lo he corregido y simplemente les he puesto un else.
 
Lo de las fisicas wops gracias por el consejo lo cambiare!!
 
 
 
Muchas gracias!!
 
 
 
Dejo el script arreglado para futuras visitas . :)
 
 
 

using UnityEngine;
using System.Collections;
public class moverplayer : MonoBehaviour
{

public float Velocidad = 1.0F; //velocidad caminando
public float Correr = 4.0F;//velocidad corriendo
private Animation anim;
public float jumpHight = 0.5F;
public float jumptime = 0.300F;
public float smoothFactor = 5;

void Start()
{
   anim = GetComponent<Animation> ();
}
 
void Update ()
{
   if (Input.GetKey (KeyCode.RightControl) && Input.GetKey (KeyCode.UpArrow)) {
      Debug.Log ("correee");
      transform.position += transform.forward * Time.deltaTime * Correr;
      //transform.Translate (Vector3.forward * Time.deltaTime * Correr);
      transform.forward = new Vector3 (0f, 0f, 1f);
      anim.Play ("run00");
   }
   // Movemos el objeto hacia adelante
   else {
      if (Input.GetKey (KeyCode.UpArrow)) {
         //transform.Translate (Vector3.forward * Time.deltaTime * Velocidad);
         transform.position += transform.forward * Time.deltaTime * Velocidad;
         transform.forward = new Vector3 (0f, 0f, 1f);
         anim.Play ("walk00");
      }
      else
      {
         if (Input.GetKey (KeyCode.DownArrow)) {
            //atras
            //transform.Translate (Vector3.back * Time.deltaTime * Velocidad, Space.World);
            transform.position += transform.forward * Time.deltaTime * Velocidad;
            transform.forward = new Vector3 (-0f, 0f, -1f);
            anim.Play ("walk00");
         }
         else
         {
            if (Input.GetKey (KeyCode.RightArrow)) {
               //derecha
               //transform.Translate (Vector3.right * Time.deltaTime * Velocidad, Space.World);
               transform.position += transform.forward * Time.deltaTime * Velocidad;
               transform.forward = new Vector3 (1f, 0f, 0f);
               anim.Play ("walk00");
            }
            else
            {
               if (Input.GetKey (KeyCode.LeftArrow)) {
                  //izquierda
                  //transform.Translate (Vector3.left * Time.deltaTime * Velocidad, Space.World);
                  transform.position += transform.forward * Time.deltaTime * Velocidad;
                  transform.forward = new Vector3 (-1f, 0f, 0f);
                  anim.Play ("walk00");
               }
               else
               {
                     anim.Play ("idle01");
               }
      
               }
            }
         }
      }
   }
}

 
 

Etiquetas: