【问题标题】:How do I stop the rotation of my gameobject when it reaches a certain point on its y-axis?当我的游戏对象到达其 y 轴上的某个点时,如何停止它的旋转?
【发布时间】:2015-07-08 19:32:19
【问题描述】:

我目前正在做一个简单的棒球比赛。我想要做的是让玩家能够向后挥动球棒以“充电”力量,可以这么说,然后当释放按钮时,他将以等于数量的速度向前挥动球棒蓄电。

这一切都很好,但我的问题是,当球棒到达 y 轴上的某个点时,我需要停止球棒的运动,我有点不确定如何去做,因为我不能只是告诉它在设定的时间后停止旋转,因为由于每次挥杆可能具有的速度差异,球棒不会每次都同时到达前点。

我想要做的基本上是if(reached 266 on the y-axis stop rotating),但我不确定如何做到这一点。

这里是我目前写的代码:

public int rotateSpeed = 50;
public float AmountOfPowerChargedUp = 0;

void Update() 
{

    if (Input.GetMouseButton(0))    
    {
        AmountOfPowerChargedUp += 5f;
        transform.Rotate(Vector3.up * rotateSpeed * Time.deltaTime); 
        Debug.Log(AmountOfPowerChargedUp);
    }

    if (!Input.GetMouseButton (0)) 
    {
        transform.Rotate(Vector3.down * AmountOfPowerChargedUp * Time.deltaTime);
    }   

}

private void OnGUI()
{
    GUI.Label (new Rect (50, 15, 250, 25), "AmountOfPowerChargedUp: " + AmountOfPowerChargedUp); 
}

【问题讨论】:

  • 我不太了解统一性,但是是否有一个名为“move”之类的事件可以订阅,然后在方法主体中测试新位置是否可以接受,例如 if (object .positoon.Y){>>}

标签: c# unity3d rotation gameobject


【解决方案1】:

如果您旋转一定度数,您应该使用SlerpRotateTowards

Vector3 targetDir = (target.position - transform.position).normalized;

// The step size is equal to speed times frame time.
float step = speed * Time.deltaTime;

Vector3 newDir = Vector3.RotateTowards(transform.up, targetDir, step, 0.0);

transform.rotation = Quaternion.LookRotation(newDir)

【讨论】:

  • 问题是我不是每次都旋转相同的度数,这取决于棒球棒被拉了多远,所以我不能那样做,我也不能用时间来测量它因为向前挥杆的速度也不同。我在这里不知所措
  • 不能用时间来衡量吗?你在说什么?每当您对运动进行迭代调用时,您都希望使用时间。另外,您想要做的是我已经说过的 Slerp,它将完全按照您的要求进行。只需从任何当前旋转到您正在寻找的最终旋转进行 Slerp,这样您就不会每次旋转相同的度数,而是您需要的确切量。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多