【问题标题】:Unity- Moving an Object to the left on a Scrolling BackgroundUnity-在滚动背景上向左移动对象
【发布时间】:2018-08-06 13:29:24
【问题描述】:

我正在尝试在 Unity 上创建一个简单的 Infinite Runner 游戏,但遇到了问题。任务是在地板上生成一个球并立即开始向左滚向玩家。我已经尝试了多种方法来实现它,但它似乎不起作用。这是我最近的尝试:

public class ObstaclePool : MonoBehaviour {

public GameObject columnPrefab;
public GameObject ballPrefab;
public int obstaclePoolSize = 5;
public float spawnRate = 3f;

private GameObject[] obstacles;
private int currentObstacle = 0;

private Vector2 objectPoolPosition = new Vector2(-15, -25);
private float timeSinceLastSpawned;

private float spawnXPosition;
private bool hasCalled = false;

private int dice;
bool beforeBall = false;
// Use this for initialization
void Start () {
    timeSinceLastSpawned = 0f;
    SetupObstacles();
}

private void SetupObstacles()
{
    obstacles = new GameObject[obstaclePoolSize];

    for (int i = 0; i < obstaclePoolSize; i++)
    {
        dice = Random.Range(1, 3);
        if (dice == 1)
        {
            obstacles[i] = (GameObject)Instantiate(columnPrefab, objectPoolPosition, Quaternion.identity);
        }
        else if (dice == 2)
        {
            obstacles[i] = (GameObject)Instantiate(ballPrefab, objectPoolPosition, Quaternion.identity);
        }
    }
}
// Update is called once per frame
void Update () {
    timeSinceLastSpawned += Time.deltaTime;

    if (GameControl.instance.gameOver == false && timeSinceLastSpawned >= spawnRate)
    {
        timeSinceLastSpawned = 0f;
        if (hasCalled == false)
        {
            spawnXPosition = 10f;
            hasCalled = true;
        }
        else
        {
            spawnXPosition = Random.Range(6f, 10f);
        }

        if (obstacles[currentObstacle].transform.tag == "Ball")
        {
            spawnXPosition = Random.Range(9f, 10f);
            obstacles[currentObstacle].transform.position = new Vector2(spawnXPosition, -1.84f);
            ballPrefab.GetComponent<Rigidbody2D>().AddForce(new Vector2(-100f, 0) * 5);
            beforeBall = true;
        }


        else {
            if (beforeBall == true)
            {
                spawnXPosition = Random.Range(9f, 10f);
                beforeBall = false;
            }

            obstacles[currentObstacle].transform.position = new Vector2(spawnXPosition, -7.08f);
            Debug.Log(spawnXPosition);
        }


        currentObstacle++;

        if (currentObstacle >= obstaclePoolSize)
        {
            currentObstacle = 0;
            SetupObstacles();
            hasCalled = false;
        }
    }
}

}

对于我的代码的快速解释:我有一个大小为 5 的数组。它包含我创建的障碍。在决定将什么放入数组时,我生成了一个随机数(1 或 2)。如果是 1,我会放在一列。如果是 2,我投进一个球。这些障碍物在屏幕外产生。然后,在使用随机数确定 X 位置后,在实际场景中移动它们。

这部分尤其是我尝试实现它的地方:

        if (obstacles[currentObstacle].transform.tag == "Ball")
        {
            spawnXPosition = Random.Range(9f, 10f);
            obstacles[currentObstacle].transform.position = new Vector2(spawnXPosition, -1.84f);
            ballPrefab.GetComponent<Rigidbody2D>().AddForce(new Vector2(-100f, 0) * 5);
            beforeBall = true;
        }

我可能有一些我一直在测试的东西的残余,所以有些代码可能看起来多余和混乱。

我也尝试使用 Translate 和 Velocity,但没有成功。我还有一个 ScrollingObject 代码和一个 RepeatingBackground 代码。我也将 ScrollingObject 代码放在 Ball 预制件中。 (另外,试着把它拿出来-> 球滚到右边)。这些代码来自 Unity 教程。

重复背景:

public class RepeatingBackground : MonoBehaviour {

private BoxCollider2D groundCollider;
private float groundHorizontalLength;
// Use this for initialization
private void Awake () {
    groundCollider = GetComponent<BoxCollider2D>();
    groundHorizontalLength = groundCollider.size.x;
}

// Update is called once per frame
private void Update () {
    if (transform.position.x < -groundHorizontalLength)
    {
        RepositionBackground();
    }
}

private void RepositionBackground()
{
    Vector2 groundOffSet = new Vector2(groundHorizontalLength * 2f, 0);
    transform.position = (Vector2)transform.position + groundOffSet;
}

}

滚动对象:

public class ScrollingObject : MonoBehaviour {

private Rigidbody2D rb2d;

// Use this for initialization
void Start () {
    rb2d = GetComponent<Rigidbody2D>();
    rb2d.velocity = new Vector2(GameControl.instance.scrollSpeed, 0);
}

// Update is called once per frame
void Update () {
    if (GameControl.instance.gameOver == true)
    {
        rb2d.velocity = Vector2.zero;
    }
}

}

【问题讨论】:

    标签: unity3d game-physics


    【解决方案1】:

    看起来您只是在第一个示例中命名了错误的对象。

            if (obstacles[currentObstacle].transform.tag == "Ball")
        {
            spawnXPosition = Random.Range(9f, 10f);
            obstacles[currentObstacle].transform.position = new Vector2(spawnXPosition, -1.84f);
            ballPrefab.GetComponent<Rigidbody2D>().AddForce(new Vector2(-100f, 0) * 5);
            beforeBall = true;
        }
    

    请注意您如何在此处将力添加到预制件,而不是预制件的实例化版本。

    【讨论】:

      【解决方案2】:

      我又玩了一会儿,让它开始工作。我刚刚编写了另一个脚本,为对象增加了力量。我仍然不明白为什么我原来的方法不起作用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多