向上重复的背景

时间:2019-05-05 23:39:10

标签: c# unity3d game-development

我希望我的玩家从一堵墙跳到另一堵(攀登),并且我希望背景滚动并向上重复。 这是我用于滚动和重复背景的代码。知道如何反转背景位置以向上重复吗?

NullReferenceException: Object reference not set to an instance of an object
Scroll.Update () (at Assets/Scripts/Scroll.cs:22)
       // If the game is over, stop scrolling.
        if (GameController.Instance.isGameOver == true)
        {

using UnityEngine;
using System.Collections;

public class RepeatingBackground : MonoBehaviour
{

    private BoxCollider2D groundCollider;       //This stores a reference to the collider attached to the Ground.
    private float groundHorizontalLength;       //A float to store the x-axis length of the collider2D attached to the Ground GameObject.

    //Awake is called before Start.
    private void Awake()
    {
        //Get and store a reference to the collider2D attached to Ground.
        groundCollider = GetComponent<BoxCollider2D>();
        //Store the size of the collider along the x axis (its length in units).
        groundHorizontalLength = groundCollider.size.x;
    }

    //Update runs once per frame
    private void Update()
    {
        //Check if the difference along the x axis between the main Camera and the position of the object this is attached to is greater than groundHorizontalLength.
        if (transform.position.x < -groundHorizontalLength)
        {
            //If true, this means this object is no longer visible and we can safely move it forward to be re-used.
            RepositionBackground();
        }
    }

    //Moves the object this script is attached to right in order to create our looping background effect.
    private void RepositionBackground()
    {
        //This is how far to the right we will move our background object, in this case, twice its length. This will position it directly to the right of the currently visible background object.
        Vector2 groundOffSet = new Vector2(groundHorizontalLength * 2f, 0);

        //Move this object from it's position offscreen, behind the player, to the new position off-camera in front of the player.
        transform.position = (Vector2)transform.position + groundOffSet;
    }
}

滚动类:

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

public class Scroll : MonoBehaviour
{
    private Rigidbody2D rb2d;

    // Use this for initialization
    void Start()
    {
        //Get and store a reference to the Rigidbody2D attached to this GameObject.
        rb2d = GetComponent<Rigidbody2D>();

        //Start the object moving.
        rb2d.velocity = new Vector2(GameController.Instance.scrollSpeed, 0);
    }

    void Update()
    {
        // If the game is over, stop scrolling.
        if (GameController.Instance.isGameOver == true)
        {
            rb2d.velocity = Vector2.zero;
        }
    }
}

现在我刚得到一个错误

0 个答案:

没有答案