沿对象在Unity中移动的方向旋转

问题描述 投票:0回答:1

我有一个简单地向前移动直到碰到另一个物体的物体,在该物体中,物理材料导致该物体从另一个物体反弹。

我不是最擅长画图,但以下是我想要做的事情的图示:

enter image description here

enter image description here

我对此的物理部分很好,但是物理材料不会旋转游戏对象,因此实际结果看起来更像这样:

enter image description here

我知道您可以通过transform轻松设置对象的旋转,但是如何获取游戏对象的移动方向,然后将旋转设置为该方向?

c# unity3d 2d
1个回答
0
投票

将它附加到更新循环中的移动游戏对象上(假设已附加刚体)

Vector3 velocity = gameObject.GetComponent<Rigidbody>().velocity;
transform.rotation = Quaternion.SetLookRotation(velocity);
© www.soinside.com 2019 - 2024. All rights reserved.