正如标题所说,改变字符控制器的高度会引起抖动问题。
例如,当我降低CharacterController的高度时,似乎CharacterController漂浮在每一个框架上,并且一直落在地上。对立面有同样的问题,不同的是对撞机其实已经通过地面了,所以它只是用物理引擎把物体推上去。
(我看到一些类似的地形问题,在这种情况下更糟,它只是通过地形)
无论如何,有人告诉我,我也必须改变CharacterController的中心,用脚的偏移量(我不知道这到底是什么),所以我试着在调整高度时改变CharacterController的中心,但是它仍然有相同的问题,没有什么不同。
这是我正在使用的代码:
void Update() {
if(Input.GetKeyDown(KeyCode.LeftControl)) {
isCrouched = !isCrouched;
}
float newHeight = isCrouched ? crouchingHeight : originalHeight;
controller.height = Mathf.Lerp(controller.height, newHeight, Time.deltaTime * smooth);
Vector3 newCenter = isCrouched ? new Vector3(0, 0.2f, 0) : Vector3.zero;
controller.center = Vector3.Lerp(controller.center, newCenter, Time.deltaTime * smooth);
float newCamPos = isCrouched ? origCamPos.y - 0.2f : origCamPos.y;
Vector3 newPos = new Vector3(cam.localPosition.x, newCamPos, cam.localPosition.z);
cam.localPosition = Vector3.Lerp(cam.localPosition, newPos, Time.deltaTime * smooth);
}
有没有办法在改变角色控制器高度的同时消除这个抖动的问题?任何建议都会很感激的。
发布于 2018-08-14 02:50:59
我想这就是你面临的问题:
您可以通过偏移CharacterController的中心来解决这个问题,这样对撞机的底部总是在您的角色的底部。具体来说,您垂直偏移中心的originalHeight
和当前高度之间的差异,除以两。
在您的代码中,替换:
Vector3 newCenter = isCrouched ? new Vector3(0, 0.2f, 0) : Vector3.zero;
controller.center = Vector3.Lerp(controller.center, newCenter, Time.deltaTime * smooth);
通过以下方式:
controller.center = Vector3.down * (originalHeight - controller.height) / 2.0f;
这就是结果:
https://gamedev.stackexchange.com/questions/159781
复制相似问题