坦克大战3D 类整理

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 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;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值