画布图像无法无缝滚动

时间:2019-03-21 19:43:57

标签: c# unity3d game-engine

美好的一天

我一直在尝试无缝地重复画布图像,但是在画布内部却没有这样做,

问题似乎在此函数的定义内: RepositionImage()

一旦将图像重新放置到画布上,它将在两个图像之间创建一个间隙。

代码如下:

using UnityEngine;
public class ImageMove:MonoBehaviour
{
    private BoxCollider2D imageCollider;
    private float imageHorizontalLength;
    [SerializeField]
    private float scrollSpeed;
    private Rigidbody2D rb2d;

    private void Start()
    {
        rb2d = GetComponent<Rigidbody2D>();
        rb2d.velocity = new Vector2(-scrollSpeed, 0);
    }

    private void Awake()
    {
        imageCollider = GetComponent<BoxCollider2D>();
        imageHorizontalLength = imageCollider.size.x;
    }

    private void Update()
    {
        if (GetComponent<RectTransform>().offsetMin.x < -imageHorizontalLength)
        {
            rb2d.velocity = Vector2.zero;
            RepositionImage();
            rb2d.velocity = new Vector2(-scrollSpeed, 0);
        }
    }

    void RepositionImage()
    {
        Vector2 offset = new Vector2(imageHorizontalLength, 0);
        GetComponent<RectTransform>().anchoredPosition = (Vector2)transform.position + offset;
    }
}

View Gap Image

1 个答案:

答案 0 :(得分:0)

您正在设置图像的位置,而应该平移它。

例如,如果图像的X位置每次低于-100都设置为100,则您将丢失,但远远低于其-100。 如果达到-100.02,则将其设置为100时,将创建0.02的间隙。

相反,将其翻译成图像的宽度: 在我刚刚给出的示例中,transform.Translate(200,0,0)将其设置为99.98而不是100,并消除了间隙的创建。