通过实例学习:游戏的存档与读档(Unity 2017.2)DAY1

1.创建工程、导入素材

2.搭建游戏场景1

3.搭建游戏场景2

4.创建射击目标

5.控制怪物的随机生成

在Target上添加一个TargetManager,用来随机生成怪物

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

public class TargetManager : MonoBehaviour {

    public GameObject[] monsters;
    public GameObject activeMaonster = null;

    private void Start()
    {
        foreach(GameObject monster in monsters)
        {
            monster.SetActive(false);
            monster.GetComponent<BoxCollider>().enabled = false;
        }
        ActivateMonster();
    }

    private void ActivateMonster()
    {
        int index = Random.Range(0, monsters.Length);
        activeMaonster = monsters[index];
        activeMaonster.SetActive(true);
        activeMaonster.GetComponent<BoxCollider>().enabled = true;
    }
}

6.使用协程控制怪物的生命周期

添加和使用怪物存活时间的协程方法

    IEnumerator AliveTimer()
    {
        yield return new WaitForSeconds(Random.Range(1, 5));
        ActivateMonster();
    }


    StartCoroutine("AliveTimer");

7.控制怪物的死亡

让怪物重复生成和消失,使用两个协程实现

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

public class TargetManager : MonoBehaviour {

    public GameObject[] monsters;
    public GameObject activeMaonster = null;

    private void Start()
    {
        foreach(GameObject monster in monsters)
        {
            monster.SetActive(false);
            monster.GetComponent<BoxCollider>().enabled = false;
        }
        //ActivateMonster();
        //调用携程
        StartCoroutine("AliveTimer");
        
    }

    //随机激活怪物
    private void ActivateMonster()
    {
        int index = Random.Range(0, monsters.Length);
        activeMaonster = monsters[index];
        activeMaonster.SetActive(true);
        activeMaonster.GetComponent<BoxCollider>().enabled = true;
        StartCoroutine("DeathTimer");
    }

    //迭代器方法,设置生成的等待时间
    IEnumerator AliveTimer()
    {
        //等待1-4秒后执行ActivateMonster方法
        yield return new WaitForSeconds(Random.Range(1, 5));
        ActivateMonster();
        
    }

    //使激活状态的怪物变为未激活状态
    private void DeActivateMonster()
    {
        if (activeMaonster != null)
        {
            
            activeMaonster.GetComponent<BoxCollider>().enabled = false;
            activeMaonster.SetActive(false);
            activeMaonster = null;
        }
        StartCoroutine("AliveTimer");
    }

    //迭代器,设置死亡的等待时间
    IEnumerator DeathTimer()
    {
        yield return new WaitForSeconds(Random.Range(3, 8));
        DeActivateMonster();
    }
}

8.设置手枪的旋转

创建GunManager,控制手枪的旋转

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

public class GunManager : MonoBehaviour {

    private float maxYRotation = 120;
    private float minYRotation = 0;
    private float maxXRotation = 60;
    private float minXRotation = 0;

    private float shootTime = 1;
    private float shootTimer = 0;

    private void Update()
    {
        shootTimer += Time.deltaTime;
        if (shootTimer >= shootTime)
        {
            //TODO:可以射击
        }
        float xPosPrecent = Input.mousePosition.x / Screen.width;
        float yPosPrecent = Input.mousePosition.y / Screen.height;

        float xAngle = - Mathf.Clamp(yPosPrecent * maxXRotation, minXRotation, maxXRotation) + 15;
        float yAngle = Mathf.Clamp(xPosPrecent * maxYRotation, minYRotation, maxYRotation) - 60;

        transform.eulerAngles = new Vector3(xAngle, yAngle, 0);
    }
}

运行结果如下

9.控制子弹的生成

 public GameObject bulletGo;
 public Transform firePosition;

 shootTimer += Time.deltaTime;
 if (shootTimer >= shootTime)
    {
        if (Input.GetMouseButtonDown(0))
        {
                GameObject bulletCurrent = GameObject.Instantiate(bulletGo, firePosition.position, Quaternion.identity);
        }
     }

运行结果如下

10.控制子弹的移动

使用刚体控制子弹的移动

//通过刚体组件给子弹添加一个正前方向上的力,以达到让子弹向前运动的效果
bulletCurrent.GetComponent<Rigidbody>().AddForce(transform.forward * 2000);
shootTimer = 0;

11.设置手枪的动画与子弹的自动销毁

让手枪播放开枪动画

gameObject.GetComponent<Animation>().Play();

创建BulletManager,并添加销毁子弹的方法

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

public class BulletManager : MonoBehaviour {

    private void Start()
    {
        StartCoroutine("DestroySelf");
    }

    IEnumerator DestroySelf()
    {
        yield return new WaitForSeconds(2);
        Destroy(this.gameObject);
    }
}

12.子弹与怪物的碰撞

添加MonsterManager,添加碰撞和死亡的方法

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

public class MonsterManager : MonoBehaviour {

    private Animation anim;

    //定义Idle状态和Die状态的动画
    public AnimationClip idleClip;
    public AnimationClip dieClip;

    private void Awake()
    {
        //获得动画组件
        anim = gameObject.GetComponent<Animation>();
        anim.clip = idleClip;
    }

    //当检测到碰撞时
    private void OnCollisionEnter(Collision collision)
    {
        //如果碰撞到的物体的标签为Bullet,就销毁它
        if(collision.collider.tag == "Bullet")
        {
            Destroy(collision.collider.gameObject);
            anim.clip = dieClip;
            anim.Play();
        }
    }

    //当怪物被Disable的时候,将默认的动画修改为idle动画
    private void OnDisable()
    {
        anim.clip = idleClip;
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值