Unity 2D - 如何在跑步游戏中放置障碍?

时间:2016-11-19 14:32:23

标签: unity3d unity5

我完成了跑步游戏。但我这样设置了障碍:

enter image description here

这种方式会使手机经常冻结。所以我做了一个像这样的障碍对象:

enter image description here

enter image description here

我想使用函数SetActive(bool)来显示/消除障碍物。最后,它如何运作是我的跑步者处于停滞状态,背景,障碍,硬币,juwel被移动。但是我的创建者脚本有一个很大的问题。

我的问题:

enter image description here

如您所见,此代码在两个障碍物前缀之间创建空白(红色框)。我找不到问题。 Plz帮助我......

创作者剧本:

using UnityEngine;
using System.Collections;

public class CsPartCreator : MonoBehaviour {
    int count = 1;
    float timer = 0.0f;

    // Use this for initialization
    void Start () {
        for (int i = 1; i < 26; i++)//set Deactive all object excluding first object (Because This object is watched at first.)
            transform.GetChild(i).gameObject.SetActive(false);
    }

    // Update is called once per frame
    void Update () {
        timer += Time.deltaTime;

        if (timer > 5f && count < 23)//every 5 seconds and count is smaller than the number of prefeb
        {
            transform.GetChild(count).gameObject.SetActive(true);//
            count++;
            timer = 0;
        }
    }
}

这是我的层次结构:

enter image description here

1 个答案:

答案 0 :(得分:0)

我认为,缺少预制件的Z值比背景更负面,或者您使用了设置为背景的排序图层。

并检查所有障碍游戏对象是否正确或在Hirachy Part0,Part1,Part2,Part3中都是这样。如果这些顺序正确,请告诉我

请检查:)

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

public class CsPartCreator : MonoBehaviour
{
    private GameObject player;

    float timer = 0.0f;

    private List<GameObject> obstacles;

    void Awake()
    {
        player = GameObject.FindWithTag("Player");
        obstacles = new List<GameObject>();
    }

    // Use this for initialization
    void Start()
    {
        for (int i = 0; i < 26; i++)//set Deactive all object excluding first object (Because This object is watched at first.)
        {
            obstacles.Add(transform.GetChild(i).gameObject);
            transform.GetChild(i).gameObject.SetActive(false);

        }
    }

    // Update is called once per frame
    void Update()
    {
        if (obstacles == null || obstacles.Count() == 0)
            return;

        var toActivate = obstacles.Where(x => x.transform.position.x > transform.position.x && x.transform.position.x < transform.position.x + 10).ToList(); // +10 is the range
        foreach (GameObject go in toActivate)
        {
            go.SetActive(true);
            obstacles.Remove(go);
        }
    }
}

将玩家游戏对象设置为“玩家”标签,如下图所示 enter image description here