【问题标题】:Move an object up/down slowly in Unity?在 Unity 中缓慢上下移动对象?
【发布时间】:2019-11-25 23:30:17
【问题描述】:

我正在尝试缓慢地上下移动我的对象(Y 轴),它可以工作,但不幸的是,对象开始从不同的位置上下移动。如何确保它从给定位置(例如 Y=10.25f)开始上下移动?

float speed = 2f;
float height = 0.05f;

void Update()
{
    Vector3 pos = transform.position;

    float newY = Mathf.Sin(Time.time * speed);

    newY = newY*height;

    transform.position = new Vector3(pos.x, newY, pos.z);
}

【问题讨论】:

  • 请不要编辑您的问题来提出完全不同的问题。否则,您将浪费那些试图回答原始问题的人的时间。
  • 只要把它放在你想要的地方。如果您希望它从 y=0.5f 开始;然后只需在您的启动方法中设置 y 值。
  • new Vector3(pos.x, newY + someOffset, pos.z)?

标签: c# unity3d


【解决方案1】:

如果我对你的理解很好,你应该像这样将 start Y 位置添加到 newY 中:

float speed = 2f;
float height = 0.05f;
float startY = 10.25f;

void Update(){
    var pos = transform.position;
    var newY = startY + height*Mathf.Sin(Time.time * speed);
    transform.position = new Vector3(pos.x, newY, pos.z);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多