在Unity中我使用这个脚本制作了一个测试精灵,但是当我快速或随机跳跃时我的角色会掉到地上,但是当他慢慢移动时保持开启但跳跃/着陆时快速下降。
using UnityEngine;
using System.Collections;
public class code : MonoBehaviour {
//void FixedUpdate() {
/*if (Input.GetKey (KeyCode.LeftArrow))
{
int Left = 1;
}
if (Input.GetKey (KeyCode.RightArrow))
{
int Right = 1;
}
if (Input.GetKey(KeyCode.UpArrow))
{
}
*/
public float speed1 = 6.0F;
public float jumpSpeed = 8.0F;
public float gravity = 20.0F;
private Vector3 moveDirection = Vector3.zero;
void FixedUpdate() {
CharacterController controller = GetComponent<CharacterController>();
// is the controller on the ground?
if (controller.isGrounded) {
//Feed moveDirection with input.
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
//Multiply it by speed.
moveDirection *= speed1;
//Jumping
if (Input.GetButton("Jump"))
moveDirection.y = jumpSpeed;
if (Input.GetKey (KeyCode.UpArrow))
moveDirection.y = jumpSpeed;
}
//Applying gravity to the controller
moveDirection.y -= gravity * Time.deltaTime;
//Making the character move
controller.Move(moveDirection * Time.deltaTime);
}
}
答案 0 :(得分:2)
这是每个物理引擎的常见问题。
物理计算是一项代价高昂的操作,因此无法每帧运行。为了减少过热(以降低精度为代价),Unity每0.02秒只更新一次物理(这个数字是可配置的)。
你可以减少这个数字,但是物理引擎的过热程度越小,它仍然无法保证100%的准确性。要达到100%的准确度,你不应该依赖物理引擎,而是自己做。
下面是检查直线飞行的子弹碰撞的代码(取自我的一个宠物项目)。
Dim bytesWanted As Integer = bytesReceivedTotal + 8092
Dim bytesToRead As Integer = If(bytesWanted > imageData.Length, imageData.Length - bytesReceivedTotal, 8092)
clientStream.Read(imageData, bytesReceivedTotal, bytesToRead)
在你的情况下,你只需要在你的角色开始跳跃以确定他是否会撞到地面时进行射线投射,如果他这样做,你就会采取措施防止他摔倒。