Part2:Unity学习笔记十三 - Space Shooter(从视频最后一课向Done_Main.unity场景修改的过程)

来源:互联网 发布:网络安全法培训ppt 编辑:程序博客网 时间:2024/06/08 07:53

从视频最后一课向Done_Main.unity场景修改的过程

第二步:我们继续向下看,DestroyByBoundary.cs脚本看来没什么变化,控制退出边界的要销毁,略过

接下来是DestroyByContact.cs脚本,依然点Find References in scene

在scene中出现了GameController物体,但是上面根本没有我们选择的脚本,这是什么鬼,于是我们选择性忽视了这件事,把上面搜索栏中的内容复制到了工程project

的搜索栏中,于是出现了上面图片中的内容,说明这个脚本被挂在这几个预设体上,看一看,不是岩石就是子弹,这也很正常,都是Enemy

既然他们都出现了,我们就先按照模型把他们都建立起来吧,先从model中拖入scene中,然后建立完成后拖入Prefabs文件夹成为预设体

2.简单来看看DestroyByContact.cs脚本中的内容

    public GameObject explosion;
    public GameObject playerExplosion;
    public int scoreValue;
    private Done_GameController gameController;

    void Start ()
    {
        GameObject gameControllerObject = GameObject.FindGameObjectWithTag ("GameController");
        if (gameControllerObject != null)
        {
            gameController = gameControllerObject.GetComponent <Done_GameController>();
        }
        if (gameController == null)
        {
            Debug.Log ("Cannot find 'GameController' script");
        }   //找到tag为GameController的物体,获取上面的Done_GameController脚本实体,至少判断了下是否存在,保证了程序的健壮
    }

    void OnTriggerEnter (Collider other)
    {
        if (other.tag == "Boundary" || other.tag == "Enemy")  //由于绑在所有的敌人和子弹预设体上,这句等于忽视了对于场地边界和敌人间的进入碰撞,就是可进入场地且敌人间不相互攻击
        {
            return;
        }

        if (explosion != null)
        {
            Instantiate(explosion, transform.position, transform.rotation); //判断不为空后 调用敌人被消灭后的粒子效果,我有个疑问,敌人的子弹的碰撞后也显示效果?,然后去子弹的预设体看了下,它是个null,好吧,那就是子弹不执行这一条,没有粒子效果
        }

        if (other.tag == "Player")
        {
            Instantiate(playerExplosion, other.transform.position, other.transform.rotation);
            gameController.GameOver();                        //如果撞到了Player,执行Player的
        }
        
        gameController.AddScore(scoreValue);
        Destroy (other.gameObject);
        Destroy (gameObject); //加分,破坏物体
    }

这里其实我觉得这个碰撞的检测放在Player和他的子弹上比较好,因为一般来说,Player和他的子弹较唯一或种类较少,也符合一般人的逻辑,出现一下打不死的Enemy或其他种类也好区分处理

还有我们运行游戏是发现Player是不死的,然后在if(other.tag == "Player")设置断点再让Player去撞岩石,断点不会触发,那就是说没成功进入这个函数,我们检查岩石和Player的碰撞体组件,发现Player的碰撞体可能由于自动调整版本的关系Is Trigger上层的Convex选项没有亮,导致Is Trigger也是暗的,我们勾选Convex选项,再运行游戏,可以了


0 0
原创粉丝点击