0 c# unity-game-engine game-physics
我拼命地寻找到解决该问题的方法,但还没有找到任何可能的办法,也许您可以帮帮我:我正在做一个简单的测试,其中一种圆柱体可以自行旋转并通过摩擦在平坦的平台上移动。我有刚体,已经附加了网格对撞机和控制旋转的脚本。它可以快慢移动,取决于其转速。脚本如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour {
public float rollingSpeed;
public float gravity;
// Update is called once per frame
void FixedUpdate()
{
transform.Rotate(rollingSpeed, 0.0f, 0.0f);
GetComponent<Rigidbody>().AddForce(0.0f, -gravity, 0.0f);
}
}
Run Code Online (Sandbox Code Playgroud)
问题是当我运行“播放”模式时,此圆柱体本身正在旋转,但保持在相同位置!(实际上,它来回移动的幅度很小),我真的不知道是什么原因,我试图增加摩擦参数,添加物理材料,甚至向下施加第二个力(如重力),但没有用。有人可以为此提供解决方案吗?非常感谢你们!
Rotate
只是在没有任何力的情况下变换椭圆坐标,AddTorque
而是用于物理运动。
例如,
GetComponent<Rigidbody>().AddTorque(new Vector3(1, 0, 0) * rollingSpeed * Time.deltatime);
Run Code Online (Sandbox Code Playgroud)
了解更多,Unity Doc。
添加有关Unity上基本物理的技巧。
请记住,当您使用时Transform
,这意味着没有物理学。您只是在更改其位置和旋转。
与物理学一起工作始终是Rigidbody
。