Advertisement
guinunez

JugadorController

Sep 19th, 2020
1,390
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.36 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using TMPro;
  5. using UnityEngine.UI;
  6.  
  7. public class JugadorController : MonoBehaviour
  8. {
  9.     public float velocidadAdelante;
  10.     public float velocidadLateral = 20.0f;
  11.  
  12.     public float[] posiciones;
  13.  
  14.     public int posActual = 1;
  15.  
  16.     [Range(0,1)]
  17.     public float interpolacion;
  18.  
  19.     public GameObject PanelGameOver;
  20.     public TMP_Text puntosUI;
  21.  
  22.    
  23.     public bool estaJugando = true;
  24.  
  25.     public int puntos = 0;
  26.  
  27.     private Rigidbody rb;
  28.  
  29.  
  30.  
  31.     // Start is called before the first frame update
  32.     void Start()
  33.     {
  34.         rb = GetComponent<Rigidbody>();
  35.     }
  36.  
  37.     private void Update()
  38.     {
  39.  
  40.         if (estaJugando)
  41.         {
  42.  
  43.             if (transform.position.x != posiciones[posActual])
  44.             {
  45.                 float nuevaPosicionX = Mathf.Lerp(transform.position.x, posiciones[posActual], interpolacion);
  46.                 transform.position = new Vector3(nuevaPosicionX, transform.position.y, transform.position.z);
  47.             }
  48.  
  49.             if (Input.GetKeyDown("left"))
  50.             {
  51.                 if (posActual > 0)
  52.                 {
  53.                     posActual--;
  54.                 }
  55.  
  56.             }
  57.             if (Input.GetKeyDown("right"))
  58.             {
  59.                 if (posActual < posiciones.Length - 1)
  60.                 {
  61.                     posActual++;
  62.                 }
  63.  
  64.             }
  65.         }
  66.     }
  67.  
  68.     void FixedUpdate()
  69.     {
  70.  
  71.         if(estaJugando) {
  72.             Vector3 movimiento = new Vector3(0.0f, 0.0f, velocidadAdelante);
  73.             rb.AddForce(movimiento);
  74.  
  75.  
  76.             if (Input.GetButtonDown("Fire1"))
  77.             {
  78.                 rb.AddForce(0.0f, 300f, 0.0f);
  79.             }
  80.         }
  81.  
  82.     }
  83.  
  84.     private void OnCollisionEnter(Collision collision)
  85.     {
  86.        
  87.         if (collision.gameObject.tag == "Obstaculo")
  88.         {
  89.             // Aca mostramos el cartel
  90.             PanelGameOver.SetActive(true);
  91.             estaJugando = false;
  92.  
  93.             GetComponent<Rigidbody>().AddForce(0.0f, 600.0f, 700.0f);
  94.  
  95.         }
  96.     }
  97.  
  98.     private void OnTriggerEnter(Collider other)
  99.     {
  100.         if(other.gameObject.tag == "Moneda")
  101.         {
  102.             puntos++;
  103.             puntosUI.text = puntos.ToString();
  104.             Destroy(other.gameObject);
  105.         }
  106.     }
  107.  
  108. }
  109.  
  110.  
  111.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement