如何以世界单位设置纹理的滚动速度?
How do I set the scroll speed of a texture in world units?
我正在尝试用来自屏幕侧面的物体制作一个无限奔跑者。但是,物体的速度与地板纹理的速度不匹配。
public GameObject floorPrefab;
public GameObject g;
float floorSpeed = 3.0;
void FixedUpdate () {
//Floor
floorSpeed += 0.001f;
floorPrefab.GetComponent<Renderer>().material.mainTextureOffset += new Vector2(floorSpeed * Time.fixedDeltaTime, 0.0f);
//GameObject
g.transform.Translate(-Vector2.right * floorSpeed * Time.fixedDeltaTime);
}
物体和 material 应该以相同的速度在 x
方向上移动。
我的地板是 Quad
和 MeshRenderer
在 x
方向上平铺 17 次。
mainTextureOffset
使用 uv 单位,Translate
使用世界 space 单位。您需要一种在它们之间进行转换的方法。
首先,您需要知道您的渲染器渲染了多少个 uv 单位。这相当于它一次显示纹理的多少,或者它在渲染器上重复多少次。
uVcount = 17f; // repeats 17 times in the scrolling direction
然后,您需要知道渲染器(在滚动方向上)以世界单位表示的长度。因为您的渲染器是 axis-aligned,这变得很容易,因为我们可以使用 renderer.bounds.size
来计算网格的体积,然后使用 renderer.bounds.size.x
来计算长度。
Renderer rend = floorPrefab.GetComponent<Renderer>();
float rendererLength = rend.bounds.size.x;
所以现在剩下的就是将 floorSpeed
从每 fixedUpdate
的世界单位转换为每 fixedUpdate
的 UV 单位,然后将其用作偏移量的变化量:
float floorSpeedUV = floorSpeed * uVcount / rendererLength;
rend.material.mainTextureOffset += new Vector2(floorSpeedUV * Time.fixedDeltaTime, 0.0f);
总的来说,这可能是这样的:
public GameObject floorPrefab;
public GameObject g;
public float uVcount = 7f; // repeats 7 times in the scrolling direction
float floorSpeed = 3.0;
private Renderer rend;
private float rendererLength;
void Start () {
rend = floorPrefab.GetComponent<Renderer>();
rendererLength = rend.bounds.size.x;
}
void FixedUpdate () {
//Floor
floorSpeed += 0.001f;
float floorSpeedUV = floorSpeed * uVcount / rendererLength;
rend.material.mainTextureOffset += new Vector2(floorSpeedUV * Time.fixedDeltaTime, 0.0f);
//GameObject
g.transform.Translate(-Vector2.right * floorSpeed * Time.fixedDeltaTime);
}
我正在尝试用来自屏幕侧面的物体制作一个无限奔跑者。但是,物体的速度与地板纹理的速度不匹配。
public GameObject floorPrefab;
public GameObject g;
float floorSpeed = 3.0;
void FixedUpdate () {
//Floor
floorSpeed += 0.001f;
floorPrefab.GetComponent<Renderer>().material.mainTextureOffset += new Vector2(floorSpeed * Time.fixedDeltaTime, 0.0f);
//GameObject
g.transform.Translate(-Vector2.right * floorSpeed * Time.fixedDeltaTime);
}
物体和 material 应该以相同的速度在 x
方向上移动。
我的地板是 Quad
和 MeshRenderer
在 x
方向上平铺 17 次。
mainTextureOffset
使用 uv 单位,Translate
使用世界 space 单位。您需要一种在它们之间进行转换的方法。
首先,您需要知道您的渲染器渲染了多少个 uv 单位。这相当于它一次显示纹理的多少,或者它在渲染器上重复多少次。
uVcount = 17f; // repeats 17 times in the scrolling direction
然后,您需要知道渲染器(在滚动方向上)以世界单位表示的长度。因为您的渲染器是 axis-aligned,这变得很容易,因为我们可以使用 renderer.bounds.size
来计算网格的体积,然后使用 renderer.bounds.size.x
来计算长度。
Renderer rend = floorPrefab.GetComponent<Renderer>();
float rendererLength = rend.bounds.size.x;
所以现在剩下的就是将 floorSpeed
从每 fixedUpdate
的世界单位转换为每 fixedUpdate
的 UV 单位,然后将其用作偏移量的变化量:
float floorSpeedUV = floorSpeed * uVcount / rendererLength;
rend.material.mainTextureOffset += new Vector2(floorSpeedUV * Time.fixedDeltaTime, 0.0f);
总的来说,这可能是这样的:
public GameObject floorPrefab;
public GameObject g;
public float uVcount = 7f; // repeats 7 times in the scrolling direction
float floorSpeed = 3.0;
private Renderer rend;
private float rendererLength;
void Start () {
rend = floorPrefab.GetComponent<Renderer>();
rendererLength = rend.bounds.size.x;
}
void FixedUpdate () {
//Floor
floorSpeed += 0.001f;
float floorSpeedUV = floorSpeed * uVcount / rendererLength;
rend.material.mainTextureOffset += new Vector2(floorSpeedUV * Time.fixedDeltaTime, 0.0f);
//GameObject
g.transform.Translate(-Vector2.right * floorSpeed * Time.fixedDeltaTime);
}