/// <summary> /// 生成一个损毁的坦克,生成位置和状态与此脚本物体一致 /// </summary> void DepolyADestroyedTank() { ctrller = tmp; float x = Random.Range(-30.0f, 30); float y = Random.Range(-15.0f, 15); switch (ctrller) { case Ctrller.player: destroyedTank = (GameObject)Resources.Load("Prefabs/Player"); CloneATank(); ctrller = Ctrller.player; break; case Ctrller.wingman: destroyedTank = (GameObject)Resources.Load("Prefabs/Wingman"); CloneATank(); ctrller = Ctrller.wingman; break; case Ctrller.enemy: GameObject GameController = GameObject.Find("GameController"); GameController.GetComponent <EnemyController>().enemyNums--; destroyedTank = (GameObject)Resources.Load("Prefabs/enemy"); CloneATank(); Destroy(this.gameObject); //敌人血量为0时,复制新坦克到原位置,原坦克销毁 break; } this.transform.position = new Vector3(x, y, this.transform.position.z); //位置恢复 this.gameObject.GetComponent <Rigidbody2D>().constraints = RigidbodyConstraints2D.None; this.gameObject.GetComponent <Rigidbody2D>().constraints = RigidbodyConstraints2D.FreezeRotation; //解冻 health = healthPre; //血量回复 bulletNum = bulleNumPre; //弹药恢复 }
/// <summary> /// 根据被击中位置,减少血量 /// </summary> /// <param name="isFront">是否为前装甲中弹</param> public void UpdateHealth(bool isFront) { if (ctrller != Ctrller.destroyed) //没有被摧毁的情况下 { if (isFront) //前装甲被击中,health.x减少damage.x血量 { health = new Vector2(health.x - damage.x, health.y); } else //后装甲被击中,health.y减少damage.y血量 { health = new Vector2(health.x, health.y - damage.y); } if (health.x < 0 || health.y < 0) //前后装甲任意一个损坏之后,不销毁车辆,冻结其位置和旋转,5秒后在原处生成一个损坏的车辆,原来的车辆恢复。 { health = new Vector2(0, 0); //装甲值清零 ctrller = Ctrller.destroyed; //取消对该脚本物体的控制 remainChance--; //剩余机会减1 if (remainChance < 0) { isDone = true; } this.gameObject.GetComponent <Rigidbody2D>().constraints = RigidbodyConstraints2D.FreezeAll; //冻结该脚本物体 Invoke("DepolyADestroyedTank", 5); //5秒后生成损坏车辆,并解冻复位 } } }
private float targetAngle; //炮塔目标角度(度数) void Start() { turret = this.transform.Find("Turret"); //获得炮塔物体 muzzle = turret.transform.Find("Muzzle"); //获得火焰物体 cannonBall = (GameObject)Resources.Load("Prefabs/CannonBalls"); //加载炮弹物体 // this.transform.localRotation = targetRotation; ctrller = this.GetComponent <Tank>().ctrller; //同步炮塔的控制者为坦克控制者 muzzle.gameObject.SetActive(false); //将火焰不可见 }
private void Awake() { health = healthPre; bulletNum = bulleNumPre; remainChance = 2; rigidbodyTank = this.transform.GetComponent <Rigidbody2D>(); audioMove = this.transform.GetComponent <AudioSource>(); tmp = ctrller; destroyedVehicle = GameObject.Find("GameController/DestroyedVehicle"); }
/// <summary> /// 修正敌人的炮塔角度,使其瞄准_ctrller /// </summary> /// <param name="_ctrller">被瞄准者</param> private void AimTo(Ctrller _ctrller) { int deviation = Random.Range(0, deviation_max); //获得偏差量 //目标位置向量减去此敌人位置向量,得到差向量 switch (_ctrller) { case Ctrller.player: vec = player.transform.position - this.transform.position; break; case Ctrller.wingman: vec = wingman.transform.position - this.transform.position; break; } //根据差向量,用反正切函数得到相对位置偏角,再用四元数targetRot表示,再加上偏差量 targetAng = 180 / Mathf.PI * Mathf.Atan2(vec.y, vec.x) + deviation; targetRot = Quaternion.Euler(0, 0, targetAng); }
void FixedUpdate() { ctrller = this.GetComponent <Tank>().ctrller; //同步炮塔的控制者为坦克控制者 switch (ctrller) { case Ctrller.player: //player开火设定 // Fire(Input.GetButtonDown("Fire1")); Fire(Input.GetKeyDown(KeyCode.K)); //按下了K键 //player转向设定 RotateTurret(Input.GetKey(KeyCode.J), Input.GetKey(KeyCode.L)); break; case Ctrller.wingman: //wingman开火 Fire(Input.GetKeyDown(KeyCode.Keypad2)); //按下了小键盘2键 //wingman转向 RotateTurret(Input.GetKey(KeyCode.Keypad1), Input.GetKey(KeyCode.Keypad3)); break; } }