|
|
- using UnityEngine;
- using System.Collections;
-
- [RequireComponent(typeof(Rigidbody2D))]
- public class PlayerController : MonoBehaviour {
-
- public GameObject bullet;
-
- public float speed;
-
- private int bulletDirection = -1;
- private Rigidbody2D rigid;
- private GameObject[] targets;
-
- // Use this for initialization
- void Start() {
- targets = GameObject.FindGameObjectsWithTag("Enemy");
-
- rigid = GetComponent<Rigidbody2D>();
- rigid.AddForce(Vector2.up * 200);
-
- }
-
- // Update is called once per frame
- void Update() {
-
- }
-
-
-
-
- public void rotatePlayer() {
- rigid.velocity = Vector3.zero;
- transform.Rotate(new Vector3(0, 0, 5f));
-
-
- }
-
- public void movePlayer() {
- rigid.AddForce(transform.right * speed,ForceMode2D.Impulse);
-
-
- }
-
-
-
- GameObject findClosestTarget(GameObject[] targets) {
- GameObject tMin = null;
- float minDist = Mathf.Infinity;
- Vector3 currentPos = transform.position;
- foreach (GameObject t in targets) {
- float dist = Vector3.Distance(t.transform.position, currentPos);
- if (dist < minDist) {
- tMin = t;
- minDist = dist;
- }
- }
- return tMin;
- }
-
- public void shoot() {
- GameObject bulletClone = (GameObject)Instantiate(bullet, transform.position + (transform.up * bulletDirection * 0.1f), transform.rotation);
- MissileController bulletScript = bulletClone.GetComponent<MissileController>();
- bulletScript.startVelocity = transform.up * bulletDirection;
- bulletDirection = -bulletDirection;
-
- bulletScript.target = findClosestTarget(targets);
- bulletScript.enabled = true;
-
- }
- }
|