C# 如何制作简单的空转摆动运动/动画?
准备看一些非常难看的代码 所以本质上我想做的是为一个统一的3d物体创建一个简单的上下摆动的运动,它在下降时变快,然后在上升时变慢。下面的代码实现了这种效果,但我知道必须有更好、更优雅的方法来实现,我就是找不到C# 如何制作简单的空转摆动运动/动画?,c#,unity3d,C#,Unity3d,准备看一些非常难看的代码 所以本质上我想做的是为一个统一的3d物体创建一个简单的上下摆动的运动,它在下降时变快,然后在上升时变慢。下面的代码实现了这种效果,但我知道必须有更好、更优雅的方法来实现,我就是找不到 public class IdleAnimation : MonoBehaviour { public float rotSpeed = 3.0f; public float minimum = 0.1f; public float maximum = 0.5f;
public class IdleAnimation : MonoBehaviour
{
public float rotSpeed = 3.0f;
public float minimum = 0.1f;
public float maximum = 0.5f;
public float yPos;
public float bounceProgress = 0;
public float initBounceSpeed = 0.001f;
private float bounceSpeed;
public float bounceAcc = 0.002f;
public float dir = 1;
// Start is called before the first frame update
void Start()
{
bounceSpeed = initBounceSpeed;
}
// Update is called once per frame
void Update()
{
//interpolates between the minimum and maximum based on the bounceProgress
yPos = Mathf.Lerp(minimum, maximum, bounceProgress);
transform.position = new Vector3(transform.position.x, yPos, transform.position.z);
//bounceProgress increases/decreases by the bounceSpeed
bounceProgress += bounceSpeed * dir;
//bounceSpeed increases/decreases by the bounceAcc
bounceSpeed += bounceAcc * dir;
//once the bounceProgress reaches 1, the direction reverses
//note that it will be going at maximum speed here
if(bounceProgress > 1)
dir = -1;
//once the bounceProgress reaches 0, the direction reverses
//note that it will be going at minimum speed here
else if (bounceProgress < 0)
{
dir = 1;
//resets bounceSpeed to avoid going into a negativeSpeed
bounceSpeed = initBounceSpeed;
}
}
}
public类IdleAnimation:monobhavior
{
公共浮子旋转速度=3.0f;
公共浮点数最小值=0.1f;
公共浮子最大值=0.5f;
公共浮点数;
公共浮动bounceProgress=0;
公共浮动初始反弹速度=0.001f;
私人浮动反弹速度;
公共浮点数c=0.002f;
公共浮动dir=1;
//在第一帧更新之前调用Start
void Start()
{
bounceSpeed=初始bounceSpeed;
}
//每帧调用一次更新
无效更新()
{
//基于bounceProgress在最小值和最大值之间插值
yPos=数学Lerp(最小值、最大值、反弹进度);
transform.position=新矢量3(transform.position.x、yPos、transform.position.z);
//bounceProgress按反弹速度增加/减少
bounceProgress+=bounceSpeed*dir;
//反弹速度按反弹速度增加/减少
bounceSpeed+=bounceAcc*dir;
//一旦反弹速度达到1,方向就会反转
//注意,它将以最大速度运行
如果(反弹进度>1)
dir=-1;
//一旦bounceProgress达到0,方向将反转
//注意,它将以最低速度运行
else if(bounceProgress<0)
{
dir=1;
//重置反弹速度以避免进入负速度
bounceSpeed=初始bounceSpeed;
}
}
}
我觉得应该有更好的方法来利用Mathf.PingPong或Mathf.SmoothStep之类的东西,我就是想不出来。感谢Abion47的帮助,我终于明白Mathf.Sin正是我所需要的
public class IdleAnimation : MonoBehaviour
{
public float rotSpeed = 3;
private float minimum = 0.1f;
private float maximum = 0.5f;
private float yPos;
private float bounceSpeed = 3;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
float sinValue = Mathf.Sin(Time.time * bounceSpeed);
yPos = Mathf.Lerp(maximum, minimum, Mathf.Abs(sinValue));
transform.position = new Vector3(transform.position.x, yPos, transform.position.z);
//Rotate
transform.Rotate(Vector3.up, Time.deltaTime * rotSpeed);
}
您正在描述一个圆弧。圆弧最好用三角函数表示。你有没有考虑过使用
Mathf.Sin
?我没想过。这似乎是正确的答案。我要试试看now@Abion47绝对难以置信,这正是我想要的。感谢您的帮助像这样使用InverseLerp
会导致变换行为在达到零时也会减慢,这就像是一个缓和函数,而您说您想要反弹。在这种情况下,我建议改为使用Mathf.Abs(sinValue)
来产生一个“反弹”的正弦波。作为一种挑剔,我想说,为了同样的目的,使用(1.0f+sinValue)/2.0f
将正弦波输出映射到0..1将比使用Mathf.InverseLerp
更有效(而且可以说是惯用的)。这就是说,这有点主观,性能提升可能可以忽略不计。只是供思考的食物。是的,这更有意义。我觉得它看起来不对劲,但我觉得我只是看得太久了。谢谢