Unity入门案例-Tanks坦克大战

这是我的第一个Unity项目精简版的坦克大战看着siki视频学着做的历时一天半。

游戏描述:

游戏由2个玩家控制,每个玩家可以控制坦克前进后退左转右转和攻击,每个玩家有100hp基础血量每次攻击随机造成10-20点伤害。当某个玩家血量低于0时他操控的坦克被摧毁并且游戏结束。

我的工作:

一、坦克的移动脚本

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TankMovement : MonoBehaviour {
    public float speed = 5;
    public float angularSpeed = 7;
    public float number = 1;
    private Rigidbody rigidbody;
    public AudioClip idleAudio;
    public AudioClip drivingAudio;
    private AudioSource audio;
	// Use this for initialization
	void Start () {
        rigidbody = this.GetComponent();
        audio = this.GetComponent();
	}

    // Update is called once per frame
    private void FixedUpdate() {
        float v = Input.GetAxis("VerticalPlayer"+number);
        float h = Input.GetAxis("HorizontalPlayer" + number);
        rigidbody.velocity = transform.forward * v * speed;
        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();
        }
	}
}

二、坦克的攻击脚本

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TankAttack : MonoBehaviour {
    private Transform firePosition;
    public GameObject shellPrefab;
    public KeyCode fireKey = KeyCode.Space;
    public float shellSpeed = 10;
    public AudioClip shotAudio;
	// Use this for initialization
	void Start () {
        firePosition = transform.Find("FirePosition");
	}
	
	// Update is called once per frame
	void Update () {
        if (Input.GetKeyDown(fireKey)) {
            AudioSource.PlayClipAtPoint(shotAudio, transform.position);
            GameObject go = GameObject.Instantiate(shellPrefab, firePosition.position, firePosition.rotation) as GameObject;
            go.GetComponent().velocity = go.transform.forward * shellSpeed;
        }
	}
}


三、坦克的血量脚本

using UnityEngine;
using UnityEngine.UI;

public class TankHealth : MonoBehaviour {
    public int hp = 100;
    public GameObject tankExplosin;
    public AudioClip tankExplosinAudio;
    public Slider hpSlider;
    private int hpTotal;
	// Use this for initialization
	void Start () {
        hpTotal = hp;
	}
	
	// Update is called once per frame
	void Update () {
		
	}
    void TakeDamage() {
        if (hp <=0) return;
        hp -= Random.Range(10, 20);
        hpSlider.value = (float)hp / hpTotal;
        if (hp <= 0) {
            AudioSource.PlayClipAtPoint(tankExplosinAudio, transform.position);
            GameObject.Instantiate(tankExplosin, transform.position + Vector3.up, transform.rotation);
            GameObject.Destroy(this.gameObject);
        }
    }
}

四、子弹的射击脚本

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Shell : MonoBehaviour {
    public GameObject shellExplosinPrefab;
    public AudioClip shellExplosinAudio;
    // Use this for initialization

    // Update is called once per frame
    private void OnTriggerEnter(Collider collider) {
        AudioSource.PlayClipAtPoint(shellExplosinAudio, transform.position);
        GameObject.Instantiate(shellExplosinPrefab, transform.position, transform.rotation);
        GameObject.Destroy(this.gameObject);
        if (collider.tag == "Tank") {
            collider.SendMessage("TakeDamage");
        }
    }
}

五、摄像机跟随脚本

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FollowTarget : MonoBehaviour {
    public Transform player1;
    public Transform player2;
    private Vector3 offset;
    private Camera camera;
	// Use this for initialization
	void Start () {
        offset = transform.position - (player1.position + player2.position) / 2;
        camera = this.GetComponent();
	}
	
	// Update is called once per frame
	void Update () {
        if (player1 == null || player2 == null) return;
        transform.position = (player1.position + player2.position) / 2 + offset;
        float dis = Vector3.Distance(player1.position, player2.position);
        float size = dis * 1.2f;
        camera.orthographicSize = size;
    }
}

六、背景音乐的设置

Unity入门案例-Tanks坦克大战_第1张图片

总结:

看视频学习前我只是简单了解一些Unity的基本操作,所以堪称小白。视频教程短短几小时的时间话费我一天多时间去学习才做出这个简陋版的坦克大战。通过这次学习我了解了一些Unity的操作如脚本的编写,其中变量的调用,控制物体的移动,利用ui表示血量,刚体的作用,摄像头的使用等等。
最后感谢siki老师的视频教程带我走入unity的世界。
 





你可能感兴趣的:(Unity小游戏)