如何在Unity中设置网格上顶点的方向?

时间:2019-01-16 19:20:56

标签: c# unity3d

我要存档以下效果: 我有一个简单的网格(正方形)。我是通过代码生成的。现在,我在正方形的每个顶点上实例化了四个小球:

   foreach(Vector3 vert in mesh.vertices) {
        Instantiate(thing, vert, transform.rotation);
    }

现在我想将这个球推向各个方向。它应该与顶点成线性关系,如下所示: What I want to archive

我尝试过:

GetComponent<Rigidbody2D>().AddRelativeForce(new Vector2(-4,1) * bulletSpeed);

在名为“ ball controller”的新脚本中,但是所有的球都向相同的方向飞行。

有什么建议吗?

1 个答案:

答案 0 :(得分:1)

AddRelativeForce只是相对于父对象的局部空间施加力。相反,您需要找出thing相对于对象中心的位置,并施加适当的力。例如:

//foreach `thing`
    GetComponent<Rigidbody2D>().AddRelativeForce((thing.transform.position-transform.position).normalized * bulletSpeed);