using UnityEngine;using System.Collections;using System.Collections.Generic;////// 2014_3_26 修改/// [AddComponentMenu("MyGame/PlayerBase")]public class PlayerBase : MonoBehaviour { //飞机控制开关 public bool _isActive=true; //飞机的速度 [SerializeField] private float _speed=10; //飞机移动的方向 public enum eDirection { X, Y, Z }; public eDirection _direction = eDirection.X; //飞机位置的限制 [SerializeField] private float _limit_min_V=-10; [SerializeField] private float _limit_max_V=10; [SerializeField] private float _limit_min_H=-10; [SerializeField] private float _limit_max_H=10; //飞机位移的数值 private float _V=0; private float _H = 0; //飞机的血值 //飞机的位置 private Transform _playerTransform; //子弹 [SerializeField] private GameObject _rocketObj; private float _rocketRate = 0;//用于子弹发射的频率 private RocketBase _rocketBase; //子弹发射的位置 我希望设置多个子弹发射的位置 不同的子弹会从不同的位置发射出来 [SerializeField] private List_listTransform = new List (); private Dictionary _rocketInstanPosition=new Dictionary (); void Start() { _playerTransform = this.transform; //找到子弹的代码 _rocketBase = _rocketObj.GetComponent (); //获取子弹的发射频率 _rocketRate = _rocketBase._rate; //定义子弹加载的位置 _rocketInstanPosition.Add("Top", _listTransform[0]); _rocketInstanPosition.Add("Left", _listTransform[1]); _rocketInstanPosition.Add("Right", _listTransform[2]); _rocketInstanPosition.Add("Bottom", _listTransform[3]); } void Update() { if (_isActive) { onPlaneAnim(); } } void onPlaneAnim() { //获取键盘的位移信息 _V = Input.GetAxis("Horizontal") * _speed * Time.deltaTime; _V = onLimit(_V, _limit_min_V, _limit_max_V); _H = Input.GetAxis("Vertical") * _speed * Time.deltaTime; _H = onLimit(_H, _limit_min_H, _limit_max_H); //控制飞机移动 switch (_direction) { case eDirection.X: this._playerTransform.Translate(new Vector3(_V, 0, _H));//position 和Translate的区别,position会受到父级的缩放影响 this._playerTransform.position = new Vector3(onLimit(this._playerTransform.position.x, _limit_min_V, _limit_max_V), this._playerTransform.position.y, onLimit(this._playerTransform.position.z, _limit_min_V, _limit_max_V)); break; case eDirection.Y: this._playerTransform.Translate(new Vector3(_V, _H, 0));//position 和Translate的区别,position会受到父级的缩放影响 this._playerTransform.position = new Vector3(onLimit(this._playerTransform.position.x, _limit_min_V, _limit_max_V), onLimit(this._playerTransform.position.y, _limit_min_V, _limit_max_V), this._playerTransform.position.z); break; case eDirection.Z: this._playerTransform.Translate(new Vector3(0, _V, _H));//position 和Translate的区别,position会受到父级的缩放影响 this._playerTransform.position = new Vector3(this._playerTransform.position.x, onLimit(this._playerTransform.position.y, _limit_min_V, _limit_max_V), onLimit(this._playerTransform.position.z, _limit_min_V, _limit_max_V)); break; } if (Input.GetKey(KeyCode.Space)) { _rocketRate -= Time.deltaTime; if (_rocketRate < 0) { _rocketRate = _rocketBase._rate; onInstantiate(); } } } float onLimit(float _num,float _min,float _max) { if (_num < _min) { _num = _min; } else if (_num > _max) { _num = _max; } return _num; } void onInstantiate() { switch (_rocketBase._planeT) { case RocketBase.PlaneTransform.Top: Instantiate(_rocketObj, _rocketInstanPosition["Top"].transform.position, _rocketInstanPosition["Top"].transform.rotation); break; case RocketBase.PlaneTransform.Left_Right: Instantiate(_rocketObj, _rocketInstanPosition["Left"].transform.position, _rocketInstanPosition["Left"].transform.rotation); Instantiate(_rocketObj, _rocketInstanPosition["Right"].transform.position, _rocketInstanPosition["Right"].transform.rotation); break; case RocketBase.PlaneTransform.Bottom: Instantiate(_rocketObj, _rocketInstanPosition["Bottom"].transform.position, _rocketInstanPosition["Bottom"].transform.rotation); break; } }}