Canvas 图片没有无缝滚动
Canvas image not scrolling seamlessly
美好的一天,
我一直在努力无缝地重复 canvas 图像,但我在 canvas、
中未能做到这一点
问题似乎出在这个函数的定义中:
重新定位图像()
一旦将图像重新定位到 canvas,它就会在两个图像之间创建间隙。
代码如下:
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
您正在设置图像的位置,而您应该在翻译它。
例如,如果您的图片的 X 位置每次低于 -100 时都设置为 100,那么无论它低于 -100 多远,您都会输。
如果它达到 -100.02,那么当你将它设置为 100 时,你将创建一个 0.02 的间隙。
相反,将其平移为图像的宽度:
transform.Translate(200,0,0) 在我刚才给出的例子中,它会把它放在 99.98 而不是 100,并否定间隙的产生。
美好的一天,
我一直在努力无缝地重复 canvas 图像,但我在 canvas、
中未能做到这一点问题似乎出在这个函数的定义中: 重新定位图像()
一旦将图像重新定位到 canvas,它就会在两个图像之间创建间隙。
代码如下:
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
您正在设置图像的位置,而您应该在翻译它。
例如,如果您的图片的 X 位置每次低于 -100 时都设置为 100,那么无论它低于 -100 多远,您都会输。 如果它达到 -100.02,那么当你将它设置为 100 时,你将创建一个 0.02 的间隙。
相反,将其平移为图像的宽度: transform.Translate(200,0,0) 在我刚才给出的例子中,它会把它放在 99.98 而不是 100,并否定间隙的产生。