Unity Spain

Post Antiguos => General (Antiguo) => Mensaje iniciado por: Jose134 en Julio 25, 2015, 05:04:44 PM

Título: Alguna forma de simplificar esto???
Publicado por: Jose134 en Julio 25, 2015, 05:04:44 PM
Hola amigos he hecho este script pero la verdad es que no me gusta como me ha quedado. Tiene que haber una forma mas sencilla pero no la veo 
using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour {

private bool wP;
private bool sP;
private bool aP;
private bool dP;

private float wT;
private float sT;
private float aT;
private float dT;

void Update () {
   if(Input.GetKeyDown(KeyCode.W)){
      transform.position += Vector3.up * 0.5f;
      wP = true;
   }
   if(Input.GetKeyDown(KeyCode.S)){
      transform.position += Vector3.down * 0.5f;
      sP = true;
   }

   if(Input.GetKeyDown(KeyCode.A)){
      transform.position += Vector3.left * 0.5f;
      aP = true;
   }
   if(Input.GetKeyDown(KeyCode.D)){
      transform.position += Vector3.right * 0.5f;
      dP = true;
   }

   if(Input.GetKeyUp(KeyCode.W)) { wP = false; wT = 0; }
   if(Input.GetKeyUp(KeyCode.S)) { sP = false; sT = 0; }
   if(Input.GetKeyUp(KeyCode.A)) { aP = false; aT = 0; }
   if(Input.GetKeyUp(KeyCode.D)) { dP = false; dT = 0; }

   if(wP){
      wT += Time.deltaTime;
      if(wT >= 0.5f) { transform.position += Vector3.up * 0.5f; wT = 0.4f; }
   }
   if(sP){
      sT += Time.deltaTime;
      if(sT >= 0.5f) { transform.position += Vector3.down * 0.5f; sT = 0.4f; }
   }
   if(aP){
      aT += Time.deltaTime;
      if(aT >= 0.5f) { transform.position += Vector3.left * 0.5f; aT = 0.4f; }
   }
   if(dP){
      dT += Time.deltaTime;
      if(dT >= 0.5f) { transform.position += Vector3.right * 0.5f; dT = 0.4f; }
   }
}
}
La P en wP sP... es de Pressed y la T de Time El script lo que hace es que cuando pulsas avanzas y si dejas pulsado avanza mas rapido... Si mal no recuerdo en pokemon se usaba algo parecido para que os hagais una idea jeje
Título: Alguna forma de simplificar esto???
Publicado por: Quel en Julio 25, 2015, 05:19:48 PM
Así en plan rápido, y para que no tenga que estar mirando cada frame si estas pulsando cada tecla, podrías poner algo como; http://docs.unity3d.com/ScriptReference/Input-anyKeyDown.html (http://docs.unity3d.com/ScriptReference/Input-anyKeyDown.html.). De este modo ahorras un poquitin de trabajo redundante.
Título: Alguna forma de simplificar esto???
Publicado por: kaito en Julio 26, 2015, 12:58:15 PM
Pues creando una clase tipo Evento, para sistematizar los eventos de KeyDown y KeyUp de una misma tecla.  
using UnityEngine;
using System.Collections;

public class KeyEvent : MonoBehaviour {

class KeyUpDown{

   KeyCode key;
   Vector3 direction;
   Transform transform;
   bool pressed;
   float time;

   public KeyUpDown(KeyCode key, Vector3 direction, Transform transform){

      this.key = key;
      this.direction = direction;
      this.transform = transform;
      this.pressed = false;
      this.time = 0f;
   
   }

   public void Update(){

      if(Input.GetKeyDown(this.key)){

         this.transform.position += Vector3.down * 0.5f;
         this.pressed = true;
         Debug.Log(this.key+" down");

      }

      if(this.pressed){

         this.time += Time.deltaTime;
         if(this.time >= 0.5f) { this.transform.position += this.direction * 0.5f; this.time = 0.4f; }

      }   

      if(Input.GetKeyUp(this.key)){

         this.pressed = false;
         this.time = 0f;
         Debug.Log(this.key+" up");

      }

   }

}

KeyUpDown A, W, S, D;

void Start(){

   this.A = new KeyUpDown(KeyCode.A, Vector3.left, this.transform);
   this.D = new KeyUpDown(KeyCode.D, Vector3.right, this.transform);
   this.S = new KeyUpDown(KeyCode.S, Vector3.down, this.transform);
   this.W = new KeyUpDown(KeyCode.W, Vector3.up, this.transform);

}

void Update(){

   this.A.Update();
   this.D.Update();
   this.S.Update();
   this.W.Update();

}

}
 
Título: Alguna forma de simplificar esto???
Publicado por: Jose134 en Julio 27, 2015, 01:06:41 PM
[quote author=kaito date=1437908295" data-ipsquote="" data-cite="kaito" class="ipsQuote">Pues creando una clase tipo Evento, para sistematizar los eventos de KeyDown y KeyUp de una misma tecla.  
using UnityEngine;using System.Collections;

public class KeyEvent : MonoBehaviour {

class KeyUpDown{

   KeyCode key;
   Vector3 direction;
   Transform transform;
   bool pressed;
   float time;

   public KeyUpDown(KeyCode key, Vector3 direction, Transform transform){

      this.key = key;
      this.direction = direction;
      this.transform = transform;
      this.pressed = false;
      this.time = 0f;
   
   }

   public void Update(){

      if(Input.GetKeyDown(this.key)){

         this.transform.position += Vector3.down * 0.5f;
         this.pressed = true;
         Debug.Log(this.key+" down");

      }

      if(this.pressed){

         this.time += Time.deltaTime;
         if(this.time >= 0.5f) { this.transform.position += this.direction * 0.5f; this.time = 0.4f; }

      }   

      if(Input.GetKeyUp(this.key)){

         this.pressed = false;
         this.time = 0f;
         Debug.Log(this.key+" up");

      }

   }

}

KeyUpDown A, W, S, D;

void Start(){

   this.A = new KeyUpDown(KeyCode.A, Vector3.left, this.transform);
   this.D = new KeyUpDown(KeyCode.D, Vector3.right, this.transform);
   this.S = new KeyUpDown(KeyCode.S, Vector3.down, this.transform);
   this.W = new KeyUpDown(KeyCode.W, Vector3.up, this.transform);

}

void Update(){

   this.A.Update();
   this.D.Update();
   this.S.Update();
   this.W.Update();

}

}
 [/quote]La verdad es que al principio me ha costado entender el script pero ahora que ya lo he mirado detenidamente el script principal se me quedaria mas... "compacto"