2

我想在我的场景中模拟“一堆”物体的涡流。

在 Unity 中我该怎么做?

谢谢

4

2 回答 2

1

如果您使用的是物理系统,则有两个部分。施加涡流力,获得漂亮的涡流效果。要施加涡流力,您可以在刚体上循环并施加力。为了使漩涡看起来像一个适当的漩涡漩涡,您需要以切向速度开始物体,您可以使用矢量叉积计算出该速度。

public float VortexStrength = 1000f;

public float SwirlStrength = 5f;

void Start () {
    foreach(GameObject g in RigidBodies){
        //to get them nice and swirly, use the perpendicular to the direction to the vortex
        Vector3 direction = Vortex.transform.position - g.transform.position;
        var tangent = Vector3.Cross(direction, Vector3.up).normalized * SwirlStrength;
        g.GetComponent<Rigidbody>().velocity = tangent;
    }
}

void Update(){
    //apply the vortex force
    foreach(GameObject g in RigidBodies){
        //force them toward the center
        Vector3 direction = Vortex.transform.position - g.transform.position;
        g.GetComponent<Rigidbody>().AddForce(direction.normalized * Time.deltaTime * VortexStrength);
    }
}
于 2018-11-12T17:31:20.147 回答
1

圆周运动:

 float angle =0;
 float speed=(2*Mathf.PI)/5 //2*PI in degress is 360, so you get 5 seconds to complete a circle
 float radius=5;
 void Update()
 {
     angle += speed*Time.deltaTime; //if you want to switch direction, use -= instead of +=
     x = Mathf.Cos(angle)*radius;
     y = Mathf.Sin(angle)*radius;
 }

你的圆心就是你的漩涡的中心。

当然:

  • 如果你想要多个与涡旋中心距离不同的物体,
    你必须使用你的半径变量(我会添加一个
    Random.Range(minDistance,maxDistance))

  • 如果您想要不同的速度,您可以随机化/更改速度。

  • 如果您不想要一个完美的圆圈,请随机化/更改您的 x/y

希望我很清楚。

于 2018-11-12T16:30:46.637 回答