C# 游戏对象来回旋转
我正在为我的比赛在草地上工作,那里需要看起来有风。所以我试着写剧本,但没有成功。脚本将类似于它旋转一点的位置,然后以另一种方式旋转。只是来来回回C# 游戏对象来回旋转,c#,unity3d,C#,Unity3d,我正在为我的比赛在草地上工作,那里需要看起来有风。所以我试着写剧本,但没有成功。脚本将类似于它旋转一点的位置,然后以另一种方式旋转。只是来来回回 public GameObject grass; private void FixedUpdate() { grass.transform.Rotate(transform.rotation.x, transform.rotation.y, 90f * Time.deltaTime, Space.Self);
public GameObject grass;
private void FixedUpdate()
{
grass.transform.Rotate(transform.rotation.x, transform.rotation.y, 90f * Time.deltaTime, Space.Self);
}
正如你在我的代码中看到的,它将永远围绕自己旋转。这正是我所得到的。我从来没有做过与团结有关的事情,但你可以尝试这样的事情 如果你想要一个好的波浪效果,你应该使用一个
Math.Sin()
。可能是这样的:
public GameObject grass;
private double _time;
private void FixedUpdate()
{
_time = _time + Time.deltaTime;
// I assumed that the angle for unity is in degrees.
// Math.Sin work with Radians. a full wave will take about 6 seconds.
// It will go between 0 and 90 degrees.
var angle = (float)(45 + (45 * Math.Sin(_time)));
grass.transform.Rotate(transform.rotation.x, transform.rotation.y, angle, Space.Self);
}
如果希望旋转角度从-45度到45度,请使用:
(float)(45*Math.Sin(_-time))
首先,您不希望在FixedUpdate
中执行此操作,而是希望在Update
中执行此操作,否则您会感到不安
然后,您可以简单地定义两个目标旋转并将其用作因子,以便在两个旋转之间来回插值:
public Vector3 eulerAngles1;
public Vector3 eulerAngles2;
// How long it takes to go from eulerAngles1 to eulerAngles2
public float duration;
Quaternion rotation1;
Quaternion rotation2;
private void Start()
{
rotation1 = Quaternion.Euler(eulerAngles1);
rotation2 = Quaternion.Euler(eulerAngles2);
}
private void Update()
{
var factor = Marhf.PingPong(Time.time / duration, 1);
// Optionally you can even add some ease-in and -out
factor = Mathf.SmoothStep(0, 1, factor);
// Now interpolate between the two rotations on the current factor
transform.rotation = Qiaternion.Slerp(rotation1, rotation2, factor);
}