控制坦克的移动
public class moveRen : MonoBehaviour {
public float moveSpeed = 3f;
public float roateSpeed = 9f;
private Rigidbody target;
void Start () {
target = this.GetComponent<Rigidbody> ();
}
void FixedUpdate () {
float v = Input.GetAxis ("Vertical");
target.velocity = transform.right * v * moveSpeed;
float h = Input.GetAxis ("Horizontal");
target.angularVelocity = transform.up * h * roateSpeed;
}
}
发射子弹和自动发射子弹
public class TankShell : MonoBehaviour {
public GameObject shell;
public Transform shellPosition;
public float shellSpeed = 10;
private int count = 0;
public bool isAuto = false;
void shoot()
{
GameObject go = GameObject.Instantiate (shell, shellPosition.position, shellPosition.rotation) as GameObject;
Rigidbody r = go.GetComponent<Rigidbody> ();
r.velocity = shellPosition.forward * shellSpeed;
}
void FixedUpdate () {
if (isAuto) {
count++;
if (count > 100) {
count = 0;
shoot ();
}
} else {
if (Input.GetKeyDown (KeyCode.Space)) {
shoot ();
}
}
}
}
public class PengZhuang : MonoBehaviour {
public GameObject explosion;
// void OnCollisionEnter(Collision other)
// {
// //销毁脚本附加的物体
// Destroy (gameObject);
// GameObject obj = GameObject.Instantiate (explosion,transform.position,transform.rotation);
// Destroy (obj,1f);
// }
void OnTriggerEnter(Collider other)
{
//销毁脚本附加的物体
Destroy (gameObject);
GameObject obj = GameObject.Instantiate (explosion,transform.position,transform.rotation);
Destroy (obj,1f);
if (other.tag == "Tank") {
other.SendMessage ("TakeDamage");
}
}
public class XueLiang : MonoBehaviour {
public float health = 100;
// public void TakeDamage(float value)
// {
// if (health > value) {
// health -= value;
// } else {
// Destroy (gameObject);
// }
// }
public void TakeDamage()
{
if (health > 0) {
health -= 100;
}
if (health <= 0) {
GameObject.Destroy (this.gameObject);
}
}
}