Advertisement
SySKNoT

ControllerTank

Mar 3rd, 2015
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.01 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4.  
  5. /// <summary>
  6. /// Key movement. Que mueva un objeto cuando se pulsen teclas
  7. /// </summary>
  8.  
  9. public class Controller : MonoBehaviour {
  10.    
  11.    
  12.     public float speed =5.0f;
  13.     public float angularSpeed = 50.0f;
  14.     public float angularSpeedCal = 15.0f;
  15.    
  16.     public GameObject calibrator;
  17.     public GameObject barcaza;
  18.     public GameObject torreta;
  19.    
  20.     public float bulletSpeed =500.0f;
  21.     public GameObject missile; 
  22.    
  23.     private float fuerzaDisparo;
  24.     private bool disparando;
  25.     private bool dispara;
  26.    
  27.     // Use this for initialization
  28.     void Start ()
  29.     {
  30.         fuerzaDisparo = 0.0f;
  31.         disparando = false;
  32.         dispara = false;
  33.     }
  34.    
  35.     // Update is called once per frame
  36.     void Update ()
  37.        
  38.     {
  39.         //transform.position += Vector3.one * speed * Time.deltaTime;
  40.        
  41.         //Aki se mueve la barcaza del tanke
  42.         if (Input.GetKey (KeyCode.UpArrow))
  43.         {
  44.             transform.position += transform.forward * speed * Time.deltaTime;
  45.         }
  46.        
  47.         if (Input.GetKey (KeyCode.DownArrow))
  48.         {
  49.             transform.position -= transform.forward * speed * Time.deltaTime;
  50.         }
  51.         if (Input.GetKey (KeyCode.LeftArrow))
  52.         {
  53.             transform.Rotate(new Vector3(0, -angularSpeed *  Time.deltaTime, 0));
  54.         }
  55.         if (Input.GetKey (KeyCode.RightArrow))
  56.         {
  57.             transform.Rotate(new Vector3(0, angularSpeed *  Time.deltaTime, 0));
  58.         }
  59.        
  60.        
  61.         //Aki se mueve el calibrador
  62.         if (Input.GetKey (KeyCode.W))
  63.         {
  64.             if(calibrator.transform.rotation.x > -0.31f)
  65.                 calibrator.transform.Rotate(new Vector3(-angularSpeedCal *  Time.deltaTime, 0, 0));
  66.         }
  67.         else if (Input.GetKey (KeyCode.S))
  68.         {
  69.             if(calibrator.transform.rotation.x < 0.31f)
  70.                 calibrator.transform.Rotate(new Vector3(angularSpeedCal *  Time.deltaTime, 0 , 0));
  71.         }
  72.        
  73.         //Aki se mueve la torreta
  74.         else if (Input.GetKey (KeyCode.A))
  75.         {
  76.             torreta.transform.Rotate(new Vector3( 0,-angularSpeedCal *  Time.deltaTime, 0));
  77.         }
  78.         else if (Input.GetKey (KeyCode.D))
  79.         {
  80.             torreta.transform.Rotate(new Vector3( 0,angularSpeedCal *  Time.deltaTime, 0));
  81.         }
  82.        
  83.         //Debug.Log (calibrator.transform.rotation.x);
  84.         //Debug.Log (torreta.transform.rotation.x);
  85.        
  86.        
  87.         //El disparo del artefacto
  88.         if(Input.GetKeyDown(KeyCode.Space))
  89.         {
  90.             if (disparando == true && dispara == false)
  91.             {
  92.                 dispara = true;
  93.                 disparando = false;
  94.             }
  95.             else if (disparando == false)
  96.                 disparando = true;
  97.         }
  98.        
  99.        
  100.         if (disparando && !dispara)
  101.         {
  102.             fuerzaDisparo +=  Time.deltaTime;
  103.             //Debug.Log (fuerzaDisparo);
  104.         }
  105.        
  106.         if (dispara)
  107.         {
  108.             GameObject bulletObject = Instantiate(missile, calibrator.transform.position + transform.forward *1, Quaternion.identity) as GameObject;
  109.             ShootBullet( bulletObject, Mathf.Clamp(fuerzaDisparo,1.0f,2.5f));
  110.            
  111.             dispara = false;
  112.             fuerzaDisparo = 0.0f;
  113.         }
  114.        
  115.        
  116.     }
  117.    
  118.     public void ShootBullet(GameObject bulletObject, float fd)
  119.     {
  120.         //Este metodo Dispara las balas
  121.         Rigidbody bulletRigidbody = bulletObject.rigidbody;
  122.         bulletRigidbody.AddForce(calibrator.transform.forward * bulletSpeed * fd);
  123.        
  124.     }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement