我正在制作3D平台游戏,并希望我的Game Object与CharacterController一起跳跃。我面临一个奇怪的问题,尽管我的调试文本说我的对象在y方向受到向量,但我在屏幕上看不到这些结果。有许多资源似乎可以用来完成这项工作 - 但这些方法都不适用于我。我以为我会在这里问。
该角色是游戏对象的集合,其中包含Transform(duh),Character Controller,RigidBody和附加的脚本。
Rigidbody有" isKinematic"检查。 RigidBody中的所有其余部分都是默认的。
以下是我的角色在我的更新循环中跳转,旋转和移动的解决方案的显着部分:
首先这是最重要的:
private Vector3 forward;
private Vector3 upvec = Vector3.zero;
然后这是我的更新:
void Update(){
CharacterController controller = GetComponent<CharacterController> ();
forward = Vector3.zero; //forward is a private Vector3 defined above
if (Input.GetKey ("w")) { forward = transform.TransformDirection (Vector3.forward); }
if (Input.GetKey("s")) { forward = transform.TransformDirection (Vector3.forward); }
if (Input.GetKey("space")) {upvec.y = jumpSpeed; //set to non-zero, I swear}
transform.Rotate (0, Input.GetAxis ("Horizontal") * rotateSpeed, 0);//rotate
float curSpeed = speed * Input.GetAxis ("Vertical");//allows backwards
text_3.text = (forward * curSpeed).ToString (); //my own debugging
controller.Move (forward * curSpeed); //moves backwards/forwards
text_1.text = upvec.ToString ();
controller.Move (upvec * Time.deltaTime); //SHOULD move upwards but does not
}
角色转身并向前/向后移动就好了。它只是不会跳。
我试图将重要的代码隔离开以说明问题。请告诉我,如果有什么我可以做的来澄清更多。
由于
答案 0 :(得分:1)
您的向上移动与帧率相关
controller.Move (upvec * Time.deltaTime)
但你的前进动作不是:
float curSpeed = speed * Input.GetAxis ("Vertical");//allows backwards
controller.Move (forward * curSpeed); //moves backwards/forwards
如果你的帧率很高,比如60 FPS
你的向前运动将是你向上运动的60倍,导致几乎没有明显的向上运动。