Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- [System.Serializable]
- public class Boundary
- {
- public float xMin, xMax, zMin, zMax;
- }
- public class PlayerController : MonoBehaviour
- {
- public float speed;
- public float tilt;
- public float fireRate;
- public Boundary boundary;
- public GameObject shot;
- public Transform shotSpawn;
- private float nextFire;
- private AudioSource audioSource;
- private Rigidbody rb;
- // Use this for initialization
- void Start ()
- {
- audioSource = GetComponent<AudioSource>();
- rb = GetComponent<Rigidbody>();
- }
- // Update is called once per frame
- void Update()
- {
- if (Input.GetButton ("Fire1") && Time.time > nextFire)
- {
- nextFire = Time.time + fireRate;
- Instantiate(shot, shotSpawn.position, shotSpawn.rotation);
- audioSource.Play();
- }
- }
- // Use this for physics based movement
- void FixedUpdate()
- {
- float moveHorizontal = Input.GetAxisRaw("Horizontal");
- float moveVertical = Input.GetAxisRaw("Vertical");
- Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
- rb.velocity = movement * speed;
- rb.position = ClampPosition(rb.position, boundary);
- rb.rotation = Quaternion.Euler(0.0f, 0.0f, rb.velocity.x * -tilt);
- }
- Vector3 ClampPosition(Vector3 currentPos,Rect boundry) {
- Vector3 newPos = currentPos;
- newPos.x = Mathf.Clamp(currentPos.x, boundry.xMin, boundry.xMax);
- newPos.z = Mathf.Clamp(currentPos.z, boundry.zMin, boundry.zMax);
- return newPos;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement