function Update ()
{
var MoveSpeed=5;
var RotateSpeed=20;
if(Input.GetKey(KeyCode.W))
{
this.transform.Translate(Vector3.forward*Time.deltaTime*MoveSpeed);
}
else if(Input.GetKey(KeyCode.S))
{
this.transform.Translate(Vector3.forward*Time.deltaTime*-MoveSpeed);
}
else if(Input.GetKey(KeyCode.A))
{
this.transform.Rotate(Vector3.up*Time.deltaTime*-RotateSpeed);
}
else if(Input.GetKey(KeyCode.D))
{
this.transform.Rotate(Vector3.up*Time.deltaTime*RotateSpeed);
}
}
public class TankMovement : MonoBehaviour {
public float speed = 5;
public float angularSpeed = 30;
public float number = 1; //增加一个玩家的编号,通过编号区分不同的控制
public AudioClip idleAudio;
public AudioClip drivingAudio;
private AudioSource audio;
private Rigidbody rigidbody;
void Start () {
rigidbody = this.GetComponent
audio = this.GetComponent
}
void FixedUpdate() {
float v = Input.GetAxis("VerticalPlayer"+number);
rigidbody.velocity = transform.forward*v*speed;
float h = Input.GetAxis("HorizontalPlayer"+number);
rigidbody.angularVelocity = transform.up*h*angularSpeed;
if (Mathf.Abs(h) > 0.1 || Mathf.Abs(v) > 0.1) {
audio.clip = drivingAudio;
if(audio.isPlaying==false)
audio.Play();
}
else {
audio.clip = idleAudio;
if (audio.isPlaying == false)
audio.Play();
}
}
}
public class TankAttack : MonoBehaviour {
private Transform firePosition;
public GameObject shellPrefab;
public KeyCode fireKey = KeyCode.Space;
public float shellSpeed = 10;
public AudioClip shotAudio;
void Start () {
firePosition = transform.Find("FirePosition");
}
void Update () {
if (Input.GetKeyDown(fireKey)) {
AudioSource.PlayClipAtPoint(shotAudio,transform.position);
GameObject go = GameObject.Instantiate(shellPrefab, firePosition.position, firePosition.rotation) as GameObject;
go.GetComponent
}
}
}
public class Shell : MonoBehaviour
{
public GameObject shellExplosionPrefab;
public AudioClip shellExplosionAudio;
public void OnTriggerEnter(Collider collider)
{
AudioSource.PlayClipAtPoint(shellExplosionAudio, transform.position);
GameObject.Instantiate(shellExplosionPrefab, transform.position, transform.rotation);
GameObject.Destroy(this.gameObject);
if (collider.tag == "Tank")
{
collider.SendMessage("TakeDamage");
}
}
}
//相机跟随两个目标的移动
public class FollowTarget : MonoBehaviour {
public Transform player1;
public Transform player2;
private Vector3 offset;
private Camera camera;
void Start () {
offset = transform.position - (player1.position + player2.position)/2;
camera = this.GetComponent
}
void Update () {
if (player1 == null || player2 == null) return;
transform.position = (player1.position + player2.position)/2 + offset;
float distance = Vector3.Distance(player1.position, player2.position);
float size = distance*0.58f;
camera.orthographicSize = size;//相机的正交尺寸:视野大小除以距离;
}
}
using UnityEngine.UI;
public class TankHealth : MonoBehaviour {
public int hp = 100;
public GameObject tankExplosion;
public AudioClip tankExplosionAudio;
public Slider hpSlider;
private int hpTotal;
void Start () {
hpTotal = hp;
}
void TakeDamage() {
if (hp <= 0) return;
hp -= Random.Range(10, 20);
hpSlider.value = (float)hp /hpTotal;
if (hp <= 0) {//收到伤害之后 血量为0 控制死亡效果
AudioSource.PlayClipAtPoint(tankExplosionAudio,transform.position);
GameObject.Instantiate(tankExplosion, transform.position + Vector3.up, transform.rotation);
GameObject.Destroy(this.gameObject);
}
}
}
public class DestroyForTime : MonoBehaviour {
public float time;
void Start () {
Destroy(this.gameObject,time);
}
}