using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PlayerMove : MonoBehaviour
{
public int speed;
public int bulletSpeed;
public Transform firePoint;
public GameObject bullet;
public int hp1 = 100;
public Slider hpSlider;
public int hpTotal;
public AudioClip idleAudio;
public AudioClip drivingAudio;
private AudioSource audio;
public AudioClip fire;
// Start is called before the first frame update
void Start()
{
hpTotal = hp1;
hpSlider.value = 1;
audio = GetComponent<AudioSource>();
}
// Update is called once per frame
void Update()
{
Vector3 moveVectorHoirzontal = Vector3.right * Input.GetAxis("Horizontal")*-1;
Vector3 moveVectorVertical = Vector3.forward * Input.GetAxis("Vertical")*-1;
Vector3 finalDirection = moveVectorHoirzontal + moveVectorVertical;
if (!(Input.GetAxis("Horizontal") == 0 && Input.GetAxis("Vertical") == 0))
{
audio.clip = drivingAudio;
if(audio.isPlaying==false)
audio.Play();
Quaternion target = Quaternion.LookRotation(finalDirection);
this.transform.rotation = Quaternion.Slerp(this.transform.rotation, target, 1);
}
else
{
audio.clip = idleAudio;
if(audio.isPlaying == false)
audio.Play();
}
//Debug.Log("mmoveVectorHorizontal:"+moveVectorHoirzontal);
//Debug.Log("moveVectorVertical:"+moveVectorVertical);
//Debug.Log("finalDirection:"+finalDirection);
// Debug.Log("rotation:" + this.transform.rotation);
this.transform.Translate(this.transform.right * Input.GetAxis("Horizontal")* Time.deltaTime * speed);
this.transform.Translate(this.transform.forward * Input.GetAxis("Vertical") * Time.deltaTime * speed*-1);
if (Input.GetKeyUp("space"))
{
AudioSource.PlayClipAtPoint(fire, transform.position);
GameObject bulletReturn = GameObject.Instantiate(bullet,firePoint.position,firePoint.rotation);
}
}
}
Player2Move类
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Player2Move : MonoBehaviour
{
public int speed;
public int bulletSpeed;
public Transform firePoint;
public GameObject bullet;
public int hp2 = 100;
public Slider hpSlider;
public int hpTotal;
// Start is called before the first frame update
public AudioClip idleAudio;
public AudioClip drivingAudio;
private AudioSource audio;
public AudioClip fire;
void Start()
{
hpTotal = hp2;
hpSlider.value = 1;
audio = GetComponent<AudioSource>();
}
// Update is called once per frame
void Update()
{
Vector3 moveVectorHoirzontal = Vector3.right * Input.GetAxis("Horizontal2") * -1;
Vector3 moveVectorVertical = Vector3.forward * Input.GetAxis("Vertical2") * -1;
Vector3 finalDirection = moveVectorHoirzontal + moveVectorVertical;
if (!(Input.GetAxis("Horizontal2") == 0 && Input.GetAxis("Vertical2") == 0))
{
audio.clip = drivingAudio;
if (audio.isPlaying == false)
audio.Play();
Quaternion target = Quaternion.LookRotation(finalDirection);
this.transform.rotation = Quaternion.Slerp(this.transform.rotation, target, 1);
}
else
{
audio.clip = idleAudio;
if (audio.isPlaying == false)
audio.Play();
}
//Debug.Log("mmoveVectorHorizontal:"+moveVectorHoirzontal);
//Debug.Log("moveVectorVertical:"+moveVectorVertical);
//Debug.Log("finalDirection:"+finalDirection);
// Debug.Log("rotation:" + this.transform.rotation);
this.transform.Translate(this.transform.right * Input.GetAxis("Horizontal2") * Time.deltaTime * speed);
this.transform.Translate(this.transform.forward * Input.GetAxis("Vertical2") * Time.deltaTime * speed * -1);
if (Input.GetKeyUp("enter"))
{
AudioSource.PlayClipAtPoint(fire, transform.position);
GameObject bulletReturn = GameObject.Instantiate(bullet, firePoint.position, firePoint.rotation);
}
}
}
BulletFly类
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class BulletFly : MonoBehaviour
{
private GameObject tank;
private Vector3 dir;
public GameObject explosionEffect;
public GameObject self;
public AudioClip explosion;
public AudioClip tankDie;
// Start is called before the first frame update
void Start()
{
tank = GameObject.FindWithTag("player1");
dir = tank.transform.forward;
//Debug.Log("dir:"+dir);
}
// Update is called once per frame
void Update()
{
this.transform.Translate(dir * Time.deltaTime * 50, Space.World);
}
private void OnCollisionEnter(Collision collision)
{
// Debug.Log("Collision");
if (!(collision.gameObject.tag == "player1"))
{
AudioSource.PlayClipAtPoint(explosion, transform.position);
GameObject.Instantiate(explosionEffect, transform.position, transform.rotation,this.transform);
Destroy(self);
}
if (collision.gameObject.tag == "player2")
{
collision.gameObject.GetComponent<Player2Move>().hp2 -= 10;
collision.gameObject.GetComponent<Player2Move>().hpSlider.value = (float)(collision.gameObject.GetComponent<Player2Move>().hp2) / collision.gameObject.GetComponent<Player2Move>().hpTotal;
//collision.gameObject.
if (collision.gameObject.GetComponent<Player2Move>().hp2 == 0)
{
AudioSource.PlayClipAtPoint(tankDie, transform.position);
Destroy(collision.gameObject);
}
}
}
}
Bullet2Fly类
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Bullet2Fly : MonoBehaviour
{
private GameObject tank;
private Vector3 dir;
public GameObject explosionEffect;
public GameObject self;
public AudioClip explosion;
public AudioClip tankDie;
// Start is called before the first frame update
void Start()
{
tank = GameObject.FindWithTag("player2");
dir = tank.transform.forward;
//Debug.Log("dir:"+dir);
}
// Update is called once per frame
void Update()
{
this.transform.Translate(dir * Time.deltaTime * 50, Space.World);
}
private void OnCollisionEnter(Collision collision)
{
// Debug.Log("Collision");
if (!(collision.gameObject.tag == "player2"))
{
AudioSource.PlayClipAtPoint(explosion, transform.position);
GameObject.Instantiate(explosionEffect, transform.position, transform.rotation,this.transform);
Destroy(self);
}
if (collision.gameObject.tag == "player1")
{
collision.gameObject.GetComponent<PlayerMove>().hp1-= 10;
collision.gameObject.GetComponent<PlayerMove>().hpSlider.value = (float)(collision.gameObject.GetComponent<PlayerMove>().hp1) / collision.gameObject.GetComponent<PlayerMove>().hpTotal;
if (collision.gameObject.GetComponent<PlayerMove>().hp1 == 0)
{
AudioSource.PlayClipAtPoint(tankDie, transform.position);
Destroy(collision.gameObject);
}
}
}
}
CameraMove类
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraMove : MonoBehaviour
{
// Start is called before the first frame update
public Transform player1Position;
public Transform player2Position;
private Vector3 dis;
void Start()
{
dis = this.transform.position - (player1Position.position + player2Position.position) / 2;
}
// Update is called once per frame
void Update()
{
transform.position = (player1Position.position + player2Position.position) / 2 + dis;
}
}
PlayerMove类using System.Collections;using System.Collections.Generic;using UnityEngine;using UnityEngine.UI;public class PlayerMove : MonoBehaviour{ public int speed; public int bulletSpeed; public Transform firePoint; public GameO