Advertisement
Guest User

Untitled

a guest
Jun 26th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.60 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. [System.Serializable]
  4. public class Boundary
  5. {
  6.     public float xMin, xMax, zMin, zMax;
  7. }
  8. public class PlayerController : MonoBehaviour
  9. {
  10.     public float speed;
  11.     public float tilt;
  12.     public float fireRate;
  13.  
  14.     public Boundary boundary;
  15.     public GameObject shot;
  16.     public Transform shotSpawn;
  17.  
  18.     private float nextFire;
  19.     private AudioSource audioSource;
  20.     private Rigidbody rb;
  21.  
  22.     // Use this for initialization
  23.     void Start ()
  24.     {
  25.         audioSource = GetComponent<AudioSource>();
  26.         rb = GetComponent<Rigidbody>();
  27.     }
  28.    
  29.     // Update is called once per frame
  30.     void Update()
  31.     {
  32.         if (Input.GetButton ("Fire1") && Time.time > nextFire)
  33.         {
  34.             nextFire = Time.time + fireRate;
  35.             Instantiate(shot, shotSpawn.position, shotSpawn.rotation);
  36.             audioSource.Play();
  37.         }
  38.     }
  39.  
  40.     // Use this for physics based movement
  41.     void FixedUpdate()
  42.     {
  43.         float moveHorizontal = Input.GetAxisRaw("Horizontal");
  44.         float moveVertical = Input.GetAxisRaw("Vertical");
  45.  
  46.         Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
  47.         rb.velocity = movement * speed;
  48.         rb.position = ClampPosition(rb.position, boundary);
  49.  
  50.         rb.rotation = Quaternion.Euler(0.0f, 0.0f, rb.velocity.x * -tilt);
  51.     }
  52.  
  53.     Vector3 ClampPosition(Vector3 currentPos,Rect boundry) {
  54.         Vector3 newPos = currentPos;
  55.         newPos.x = Mathf.Clamp(currentPos.x, boundry.xMin, boundry.xMax);
  56.         newPos.z = Mathf.Clamp(currentPos.z, boundry.zMin, boundry.zMax);
  57.         return newPos;
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement