简体   繁体   English

团结:将球朝桌扇的前进方向移动

[英]Unity : Move the ball in the forward direction of a tablefan

I am creating a VR application in which I have a tablefan and a ball with rigidbodies and colliders attached to them. 我正在创建一个VR应用程序,其中有一个桌扇和一个球,球体上附着有刚体和对撞机。 I want the ball to fly in a semicircle when it collides with the fan. 当球与风扇碰撞时,我希望球成半圆飞行。 I have done this by writing the following 2 lines in my code: 我通过在代码中编写以下两行来完成此操作:

Vector3 dir = Quaternion.AngleAxis(30, Vector3.forward) * Vector3.right;
rigidbody.AddForce(dir*hoverForce, ForceMode.Impulse);

This works fine but the ball flies just in one direction. 效果很好,但球只向一个方向飞行。 Could someone please help me understand how to find where is the fan facing and make the ball fly in the forward direction to the fan's rotation? 有人可以帮我理解如何找到风扇面对的位置并使球朝着风扇旋转的方向飞行吗?

If I use forward direction on fan's transform in Addforce, then the ball flies in a straight direction instead of at an angle. 如果在Addforce中在风扇的变换上使用正向,则球将以笔直的方向而不是倾斜的方向飞行。

Vector3 dir = Quaternion.AngleAxis(30, Vector3.forward) * Vector3.right;

the direction is constant vector here thats why its fly always in same direction. 方向是恒定矢量,这就是为什么其飞行总是沿相同方向。 you have to include transform directions in this calculation to calculate the direction according to the transform 您必须在此计算中包括变换方向,才能根据变换来计算方向

may be 也许

Vector3 dir = Quaternion.AngleAxis(30, Vector3.forward) * rigidbody.transform.right;

In order to launch the ball exactly in the direction that the fan is pointing, you need to overwrite any current velocity that the ball has. 为了准确地沿风扇指向的方向发射球,您需要覆盖球具有的任何当前速度。 For that, you can just assign to the ball's Rigidbody.velocity . 为此,您只需将其分配给球的Rigidbody.velocity Otherwise, the ball will still maintain any left-right velocity, and it can launch at a bit of an angle 否则,球仍将保持左右速度,并且可能会以一定角度发射

You can get the direction the fan is facing with fan.transform.forward , and if you want to immediately send the ball flying in that exact direction, then you can just set the velocity directly using that direction times however fast you'd like it to go: 您可以使用fan.transform.forward获取风扇面对的方向,如果您想立即发送沿该精确方向飞行的球,则可以直接使用该方向时间设置速度,而无论您希望多快去:

Rigidbody ballRB;     // given
GameObject fan;       // given
float fanLaunchSpeed; // given
ballRB.velocity =     fanLaunchSpeed * fan.transform.forward;

If you want to also add some degrees of elevation, then you can multiply that direction by Quaternion.AngleAxis(angle, fan.transform.right) : 如果您还想增加一定程度的仰角,则可以将该方向乘以Quaternion.AngleAxis(angle, fan.transform.right)

Rigidbody ballRB;     // given
GameObject fan;       // given
float fanLaunchSpeed; // given
float angle;          // given, measured in degrees
Vector3 launchDirection = Quaternion.AngleAxis(angle, fan.transform.right) 
                          * fan.transform.forward;
ballRB.velocity = fanLaunchSpeed * launchDirection;

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

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