简体   繁体   English

如何在 Unity 中获得 OnTriggerEnter 角度? C#

[英]How can i get OnTriggerEnter angle in Unity ? c#

Im making a basketball game where I put collider to The hoop and checked "is trigger" so I can detect when the ball passes throught it.我正在做一个篮球游戏,我把对撞机放在篮筐上并检查“是触发器”,这样我就可以检测到球何时穿过它。 But i want this to happen only when ball passes through it from above, not beneath.但我希望只有当球从上方穿过它时才会发生这种情况,而不是下方。 How can I solve it ?我该如何解决?

The simple solution to this problem is to use Vector3.Dot .这个问题的简单解决方案是使用Vector3.Dot This method works in such a way that when the two angles are equal, it returns the value of 1, when it is perpendicular, it returns the value of 0, and when it is opposite, it shows the value of -1, the rest is between these numbers.该方法的工作原理是,当两个角度相等时,返回值 1,垂直时返回值 0,相反时显示值 -1,其余介于这些数字之间。 Now you only need to measure the dot axis of the ball velocity with the downward direction.现在你只需要用向下的方向测量球速度的点轴。 Numbers greater than zero definitely indicate acceleration from top to bottom.大于零的数字肯定表示从上到下的加速度。

public class Ball : MonoBehaviour
{
    private void OnTriggerEnter(Collider Basket)
    {
        if (Vector3.Dot(GetComponent<Rigidbody>().velocity, Vector3.down) > 0)
        {
            Debug.Log("Goal");
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM