沿 x 轴在最小值和最大值之间移动
Move along x axis between min and max
我有一个在最小值和最大值之间移动的平面,它在两个点之间弹跳,但我不想要这种影响,我想让平面移动到一个点,然后在起点。我该怎么做?
目前我试过这个:
public class pingPongPlane : MonoBehaviour {
public float MinX = -10.2f; // y position of start point
public float MaxX = 55f; // y position of end point
public float PingPongTime = 1f; // how much time to wait before reverse
public Rigidbody rb; // reference to the rigidbody
void Update()
{
//get a value between 0 and 1
float normalizedTime = Mathf.PingPong(Time.time, PingPongTime) / PingPongTime;
//then multiply it by the delta between start and end point, and add start point to the result
float xPosition = normalizedTime * (MaxX - MinX) + MinX;
//finally update position using rigidbody
if (transform.position.x <= 8f)
rb.MovePosition(new Vector3(MaxX, rb.position.y, rb.position.z));
else
rb.MovePosition(new Vector3(xPosition, rb.position.y, rb.position.z));
}
}
使用 Mathf.Repeat 而不是 PingPong
我有一个在最小值和最大值之间移动的平面,它在两个点之间弹跳,但我不想要这种影响,我想让平面移动到一个点,然后在起点。我该怎么做?
目前我试过这个:
public class pingPongPlane : MonoBehaviour {
public float MinX = -10.2f; // y position of start point
public float MaxX = 55f; // y position of end point
public float PingPongTime = 1f; // how much time to wait before reverse
public Rigidbody rb; // reference to the rigidbody
void Update()
{
//get a value between 0 and 1
float normalizedTime = Mathf.PingPong(Time.time, PingPongTime) / PingPongTime;
//then multiply it by the delta between start and end point, and add start point to the result
float xPosition = normalizedTime * (MaxX - MinX) + MinX;
//finally update position using rigidbody
if (transform.position.x <= 8f)
rb.MovePosition(new Vector3(MaxX, rb.position.y, rb.position.z));
else
rb.MovePosition(new Vector3(xPosition, rb.position.y, rb.position.z));
}
}
使用 Mathf.Repeat 而不是 PingPong