简易枪战小游戏

//角色脚本

using UnityEngine;
using System.Collections;

public class homework05_firstperson : MonoBehaviour {

    public float moveSpeed = 10;
    public float rotateSpeed = 10;

    public Transform firePoint;
    public GameObject bulletPrefab;

    Camera mainCamera;
    CharacterController controller;

    int hp = 5;
    public static bool GameOver = false;

	void Start () {
        controller = this.GetComponent<CharacterController>();
        mainCamera = this.GetComponentInChildren<Camera>();

        //把鼠标隐藏在Game视图内,并锁定
        Cursor.lockState = CursorLockMode.Locked;
        Cursor.visible = false;
    }
	
	// Update is called once per frame
	void Update () {
        if(GameOver)
        {
            return;
        }
        else
        {
            MoveAndRotate();
            Fire();
        }
    }

    void MoveAndRotate()
    {
        float dx = Input.GetAxis("Horizontal");
        float dy = Input.GetAxis("Vertical");

        Vector3 moveDirection = transform.forward * dy + transform.right * dx;
        controller.Move(moveDirection * moveSpeed * Time.deltaTime);

        //左右旋转胶囊
        float mx = Input.GetAxis("Mouse X");
        transform.Rotate(Vector3.up, mx * rotateSpeed * Time.deltaTime);

        //上下旋转摄像机
        float my = Input.GetAxis("Mouse Y");
        mainCamera.transform.Rotate(Vector3.right, -my * rotateSpeed * Time.deltaTime);
    }

    void Fire()
    {
        if(Input.GetMouseButtonDown(0))
        {
            GameObject bullet = Instantiate(bulletPrefab);
            bullet.transform.position = firePoint.position;
            bullet.transform.rotation = firePoint.rotation;
            bullet.GetComponent<homework05_bullet>().group = Group.Player;

            //获取子弹的刚体,施加力
            Rigidbody rigid = bullet.GetComponent<Rigidbody>();
            rigid.AddForce(firePoint.forward * 100, ForceMode.Impulse);//给子弹施加力,自动获得速度
            //rigid.velocity = firePoint.forward * 8;//直接赋值速度
        }
    }

    public void OnHit()
    {
        hp--;
        if(hp<=0)
        {
            Debug.Log("游戏结束");
            GameOver = true;
            Time.timeScale = 0;
        }
    }
}

//子弹脚本

using UnityEngine;
using System.Collections;

public enum Group
{
    Enemy,
    Player,
}

public class homework05_bullet : MonoBehaviour {

    public Group group;

	// Use this for initialization
	void Start () {
        Destroy(gameObject,5);
	}
	
	// Update is called once per frame
	void OnTriggerEnter (Collider other) {

        if (group==Group.Enemy)
        {
            if (other.CompareTag("Player"))
            {
                //击中敌人,让敌人掉血

                other.GetComponent<homework05_firstperson>().OnHit();
            }
        }
        if (group == Group.Player)
        {
            if (other.CompareTag("Enemy"))
            {
                //击中敌人,让敌人掉血
                other.GetComponent<homework05_enemy>().OnHit();

            }
        }
        Destroy(gameObject);

    }
}
//敌人死亡与玩家死亡脚本

using UnityEngine;
using System.Collections;

public class homework05_enemy : MonoBehaviour {

    public Transform[] movePoints;
    public Transform firePoint;
    public GameObject bulletPrefab;

    NavMeshAgent agent;
    Transform target;

    int index = 0;
    int hp = 3;

    static int EnemyCount = 0;

    // Use this for initialization
    void Start () {
        agent = this.GetComponent<NavMeshAgent>();

        agent.SetDestination(movePoints[index].position);

        target = GameObject.Find("FirstPlayer").transform;

        EnemyCount++;
    }
	
	
	void Update () {
	    
        if(Vector3.Distance(transform.position,target.position)<10)
        {
            agent.Stop();

            transform.LookAt(target);
            //Fire();
        }
        else
        {
            agent.Resume();
        }

        //已经到达目的地
        if(!agent.pathPending && agent.remainingDistance <= agent.stoppingDistance)
        {
            index++;
            if(index > movePoints.Length-1)
            {
                index = 0;
            }
            agent.SetDestination(movePoints[index].position);
        }
	}

    float elapse = 0;
    void Fire()
    {
        elapse += Time.deltaTime;
        if(elapse>=1)
        {
            elapse = 0;
            GameObject bullet = Instantiate(bulletPrefab);
            bullet.transform.position = firePoint.position;
            bullet.transform.rotation = firePoint.rotation;
            bullet.GetComponent<homework05_bullet>().group = Group.Enemy;

            //获取子弹的刚体,施加力
            Rigidbody rigid = bullet.GetComponent<Rigidbody>();
            rigid.AddForce(firePoint.forward * 50, ForceMode.Impulse);//给子弹施加力,自动获得速度
            //rigid.velocity = firePoint.forward * 8;//直接赋值速度
        }
    }

    public void OnHit()
    {
        hp--;
        if (hp <= 0)
        {
            Destroy(gameObject);
            EnemyCount--;

            if(EnemyCount<=0)
            {
                Debug.Log("游戏胜利");
            }
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值