thieumao

PlayerController - Space Shooter

Jun 29th, 2015
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.80 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. [System.Serializable]
  5. public class Boundary
  6. {
  7.     public float xMin, xMax, zMin, zMax;
  8. }
  9.  
  10. public class PlayerController : MonoBehaviour
  11. {
  12.     public float speed;
  13.  
  14.     public float tilt;
  15.     public Boundary boundary;
  16.  
  17.     public GameObject shot;
  18.     public Transform shotSpawn;
  19.    
  20.     public float fireRate;
  21.     private float nextFire;
  22.     void Update()  
  23.     {
  24.         //Input.GetButton
  25.         //Instantiate(shot, shotSpawn.position, shotSpawn.rotation);
  26.         if (Input.GetButton("Fire1") && Time.time > nextFire) {
  27.             nextFire = Time.time + fireRate;
  28.             Instantiate(shot, shotSpawn.position, shotSpawn.rotation);
  29.             audio.Play();
  30.  
  31.  
  32.         };
  33.        
  34.         /*
  35.         if (Input.GetButton("Fire2") && Time.time > nextFire) {
  36.             nextFire = Time.time + fireRate;
  37.             Instantiate(shot, shotSpawn.position, shotSpawn.rotation);
  38.             audio.Play();
  39.         }
  40.         */
  41.     }
  42.    
  43.     void FixedUpdate ()
  44.     {
  45.         float moveHorizontal = Input.GetAxis ("Horizontal");
  46.         float moveVertical = Input.GetAxis ("Vertical");
  47.        
  48.         Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
  49.         rigidbody.velocity = movement * speed;
  50.  
  51.         // bang chuot
  52.         Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
  53.         transform.position = Vector3.Lerp(transform.position, mousePos, Time.deltaTime * speed);
  54.  
  55.         // di chuyen bang cam bien
  56.         /*
  57.         Vector3 direction = Vector3.zero;
  58.         direction.x = -Input.acceleration.z;
  59.         direction.z = Input.acceleration.y;
  60.         //* Time.deltaTime
  61.         transform.Translate (direction * speed);
  62.         */
  63.  
  64.         rigidbody.position = new Vector3
  65.             (
  66.                 Mathf.Clamp (rigidbody.position.x, boundary.xMin, boundary.xMax),
  67.                 0.0f,
  68.                 Mathf.Clamp (rigidbody.position.z, boundary.zMin, boundary.zMax)
  69.                 );
  70.        
  71.         rigidbody.rotation = Quaternion.Euler (0.0f, 0.0f, rigidbody.velocity.x * -tilt);
  72.     }
  73.  
  74. }
Advertisement
Add Comment
Please, Sign In to add comment