一、框架视图
·
二、关键代码
RobotOneController
using UnityEngine;
using System.Collections;
public class RobotOneController : MonoBehaviour {
public Transform Robot2;
public Transform HPBar;
public Transform Shild;
Animator animator;
public static float life;
float fireDistance;
float damage;
float rotateSpeed;
float stopRotation;
bool b_Attack;
public GameObject HPObj;
public GameObject LeftWeapon, RightWeapon;
void Awake()
{
animator = GetComponent<Animator> ();
life = 100;
fireDistance = 13;
damage = 0.2f;
rotateSpeed = 1.2f;
stopRotation = 3f;
b_Attack = false;
}
void Update () {
if (Robot2.gameObject.activeInHierarchy && RototTwoController.life>0) {
//保持看着敌方机器人
Quaternion rotation = Quaternion.LookRotation (Robot2.position-transform.position,transform.up);
//rotat差值
Vector3 differ = transform.rotation.eulerAngles - rotation.eulerAngles;
if (differ.magnitude>stopRotation) {
transform.rotation = Quaternion.Slerp (transform.rotation,rotation,Time.deltaTime*rotateSpeed);
} else {
transform.rotation = rotation;
//计算是否在交火距离
float distance = Vector3.Distance (transform.position,Robot2.position);
if (distance<fireDistance) {
b_Attack = true;
LeftWeapon.SetActive (true);
RightWeapon.SetActive (true);
//对敌方造成伤害
RototTwoController.life-=damage;
//当血量低于30,开启防护罩
if (life<=30) {
Shild.gameObject.SetActive (true);
}
}
else {
b_Attack = false;
LeftWeapon.SetActive (false);
RightWeapon.SetActive (false);
}
}
} else {
b_Attack = false;
Shild.gameObject.SetActive (false);
LeftWeapon.SetActive (false);
RightWeapon.SetActive (false);
}
HPObj.GetComponent<MeshRenderer>().material.SetFloat("_Float", life);
animator.SetBool ("b_Attack",b_Attack);
}
void OnEnable()
{
life = 100;
b_Attack = false;
transform.localPosition = Vector3.zero;
transform.localRotation = Quaternion.Euler (Vector3.zero);
Shild.gameObject.SetActive (false);
LeftWeapon.SetActive (false);
RightWeapon.SetActive (false);
}
}
RototTwoController
using UnityEngine;
using System.Collections;
public class RototTwoController : MonoBehaviour {
public Transform Robot1;
// public Transform Shild;
Animator animator;
public static float life;
float fireDistance;
float damage;
float rotateSpeed;
float stopRotation;
bool b_Attack;
bool b_Dead;
public GameObject HPObj;
public GameObject LeftWeapon, RightWeapon;
public GameObject ExplosionBig;
void Awake()
{
animator = GetComponent<Animator> ();
life = 100;
fireDistance = 13;
damage = 0.4f;
rotateSpeed = 0.8f;
stopRotation = 3f;
b_Attack = false;
b_Dead = false;
}
void Update () {
if (life>0) {
if (Robot1.gameObject.activeInHierarchy) {
//保持看着敌方机器人
Quaternion rotation = Quaternion.LookRotation (Robot1.position-transform.position,transform.up);
//rotat差值
Vector3 differ = transform.rotation.eulerAngles - rotation.eulerAngles;
if (differ.magnitude>stopRotation) {
transform.rotation = Quaternion.Slerp (transform.rotation,rotation,Time.deltaTime*rotateSpeed);
} else {
transform.rotation = rotation;
//计算是否在交火距离
float distance = Vector3.Distance (transform.position,Robot1.position);
if (distance<fireDistance) {
b_Attack = true;
LeftWeapon.SetActive (true);
RightWeapon.SetActive (true);
//对敌方造成伤害
if (RobotOneController.life>30) {
RobotOneController.life-=damage;
}
} else {
b_Attack = false;
LeftWeapon.SetActive (false);
RightWeapon.SetActive (false);
}
}
} else {
b_Attack = false;
b_Dead = false;
LeftWeapon.SetActive (false);
RightWeapon.SetActive (false);
}
} else {
//死亡
life = 0;
b_Attack = false;
b_Dead = true;
LeftWeapon.SetActive (false);
RightWeapon.SetActive (false);
ExplosionBig.SetActive (true);
}
HPObj.GetComponent<MeshRenderer>().material.SetFloat("_Float", life);
animator.SetBool ("b_Attack",b_Attack);
animator.SetBool ("b_Dead",b_Dead);
}
void OnEnable()
{
life = 100;
b_Attack = false;
b_Dead = false;
transform.localPosition = Vector3.zero;
transform.localRotation = Quaternion.Euler (Vector3.zero);
LeftWeapon.SetActive (false);
RightWeapon.SetActive (false);
ExplosionBig.SetActive (false);
}
}
Spawner
using UnityEngine;
using System.Collections;
public class Spawner : MonoBehaviour
{
private Transform Objectman;
private float timeSpawn = 0;
private int timeSpawnMax;
private float enemyCount = 0;
private int radiun;
private void Start()
{
if (GetComponent<Renderer>())
GetComponent<Renderer>().enabled = false;
}
private void Update()
{
GameObject[] gos = GameObject.FindGameObjectsWithTag("Enemy");
timeSpawn += 1;
if (gos.Length < enemyCount)
{
int timespawnmax = timeSpawnMax;
if (timespawnmax <= 0)
{
timespawnmax = 10;
}
if (timeSpawn >= timespawnmax)
{
GameObject enemyCreated =
(GameObject)
Instantiate(Objectman.gameObject,
transform.position +
new Vector3(Random.Range(-radiun, radiun), 20, Random.Range(-radiun, radiun)),
Quaternion.identity);
enemyCreated.transform.localScale = new Vector3(Random.Range(5, 20), enemyCreated.transform.localScale.x,
enemyCreated.transform.localScale.x);
timeSpawn = 0;
}
}
}
}