c# - Mathf.PingPong 速度问题?

标签 c# unity3d

一旦 object1 和 object2 达到彼此之间的特定距离,我们如何才能阻止 Mathf.PingPong 速度增加?

float min;
float max;

// Update is called once per frame
void Update () {
    min = object1.position.x;
    max = object2.position.x;
    transform.position = new Vector3(Mathf.PingPong(Time.time*2f, max-min)+min, transform.position.y, transform.position.z);
}

最佳答案

基本上,在像 Unity 这样的基于帧的系统中,“2f”是此类语句中的“速度”

在 PingPong 中,“2”是ping 和 pong 的时间

正如 Gunnar 解释的那样,如果您担心对象的*米每秒,则必须这样做

float desiredMPS = 10f; // you want the object to move at 10 mps
float knownDistance = max - min;
float howManySecondsForLoop = knownDistance / desiredMPS;

您可以使用“howManySecondsForLoop”作为 PingPong 的“2”。


一般来说,在特定的时间或地点改变它,

public float pongTime = 2.5f // .. or whatever as above
Vector3 p = transform.position;

float newX = Mathf.PingPong(Time.time*pongTime,max-min)+min;
p.x = newX;

transform.position = p;

并尝试自己更改“pongTime”。 (只需在编辑器中执行即可。)

在代码中,您可能会使用“Invoke”或类似的方法来更改它。

Invoke( "InThreeSecondsSlowItDown", 3f);

private void InThreeSecondsSlowItDown()
 {
 pongTime = .75f; // or calculate as above
 }

或者你可以这样做

if ( .. distance .. < .. width of enemy *2 .. )
  pongTime = pongTime * .1f; // or calculate as above

享受

简而言之,试试这个

float desiredMPS;
// you want the object to move at that many meters per second
// at first try say "3" in the Editor

void Update()
 {
 float knownDistance = max - min;
 float howManySecondsForLoop = knownDistance / desiredMPS;

 public float pongTime = howManySecondsForLoop;

 Vector3 p = transform.position;

 float newX = Mathf.PingPong(Time.time*pongTime,max-min)+min;
 p.x = newX;
 }

关于c# - Mathf.PingPong 速度问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35912458/

相关文章:

c# - 连接到集线器的 SignalR 2.2.0 WebSocket 错误

c# - ASP.NET MVC razor View ,发布到与原始模型绑定(bind)不同的模型?

unity3d - 如何下载和导入在 Unity Collab 上与您共享的 Unity 项目

c# - 沿 Y 位置移动玩家

c# - 如何平滑 Unity 3D 中的加速度计和方向传感器值?

c# - 设置任务管理器文件名

c# - 如何强制Windows Phone 8应用程序以浅色主题运行

c# - Direct X 和 WPF 集成

c++ - YUV420p 到 RGB 的转换改变了 U 和 V 值 - iOS Unity3D

c# - 在 Unity C# 中实例化游戏对象列表