循环地砖作为角色在单位2D中移动

时间:2016-05-27 17:30:25

标签: 2d unity5 tile

我正在尝试创建一个地面图块,一旦离开屏幕,就会移动到当前屏幕中另一个地面图块前面的前向位置。

enter image description here

我几乎没有在代码中尝试任何对添加到此问题稍微有用的内容。有人能指出我正确的方向能够完成这项任务,所以我不必复制数百个瓷砖吗?

谢谢!

3 个答案:

答案 0 :(得分:2)

将此脚本附加到Tile对象上。

using UnityEngine;

    public class SpawnTiles : MonoBehaviour {

        private Material currentTile;
        public float speed;
        private float offset;


        // Use this for initialization
        void Start () {

            currentTile = GetComponent<Renderer>().material;
        }

        // Update is called once per frame
        void Update () {

            offset += 0.001f;
            currentTile.SetTextureOffset ("_MainTex", new Vector2 (offset * speed, 0));
        }
    }

修改

This is what you want?

如果是,请按照这些步骤进行操作。

创建&gt; 3D对象&gt; Quad(根据需要调整大小)

enter image description here

选择你的瓷砖精灵:

纹理类型=纹理

包裹模式=重复

enter image description here

现在创建新的Material:创建&gt;材料

选择:Shader = Unlit / Texture

纹理选择以前的纹理。

enter image description here

将此Material拖放到Quad对象中。

enter image description here

在检查员处调整瓷砖。

enter image description here

结果

enter image description here

创建新脚本SpawnTiles并附加在Quad对象。

using UnityEngine;

public class SpawnTiles : MonoBehaviour {

    private Material currentTile;
    public float speed;
    private float offset;

    // Update is called once per frame
    void Update () {

        GetComponent<Renderer>().material.mainTextureOffset = new Vector2 (Time.time * speed, 0f);
    }
}

在检查员处调整运动速度。

根据需要完成重命名Quad对象。

enter image description here

答案 1 :(得分:0)

如果您只想将背面瓷砖移到前面,您可能不需要它,但我仍然强烈建议您查看object pooling

要检测相机是否无法看到图块,请使用thisRenderer.isVisible

要移动图块,如果所有图块的尺寸相同,您可以根据需要增加它的位置x。

答案 2 :(得分:0)

我找到了一个使用WorldPointToViewport()的解决方案。

这是我的剧本:

using UnityEngine;
using System.Collections;

public class groundTilingScript : MonoBehaviour {

    Transform ground; //For reference to the transform 
    Camera cam; //Reference to Main Camera

    float groundWidth; //The width of the transform, used for calculating current max x position of transform and next placement x position
    private float nextXPos = 0.0f; //Store next x position in variable for easier reading

    // Use this for initialization
    void Start () {
        //Set up References
        ground = transform;
        cam = Camera.main;

        //Store Ground width (Width of the ground tile)
        groundWidth = ground.GetComponent<Renderer> ().bounds.size.x; 
    }

    // Update is called once per frame
    void Update () {

        //Create new Vector3 to be used in WorldToViewportPoint so it doesn't use the middle of the ground as reference
        Vector3 boxRightPos = new Vector3 (ground.position.x + groundWidth/2, ground.position.y, ground.position.z);

        //Store view Position of ground
        Vector3 viewPos = cam.WorldToViewportPoint (boxRightPos);

        //If the ground tile is left of camera viewport
        if (viewPos.x < 0) {
            //gameObject is offscreen, destroy it and re-instantiate it at new xPosition
            float currentRightX = ground.position.x + groundWidth;
            nextXPos = currentRightX + groundWidth;


            Instantiate (gameObject, new Vector3 (nextXPos, ground.position.y, ground.position.z), ground.rotation);

            Destroy (gameObject);
        }

    }
}

注意:我最初必须设置两个地砖,以便为相机提供比地面砖更远的空间。 (屏幕上有一个瓷砖,一个关闭)

同样,这只是我的尝试。我对批评完全持开放态度。