2017-09-28 131 views
0

当我按下前进/后退/左/右时,玩家角色确实移动但突然停止,但是当我按下空间跳跃时,它会再次开始移动我的代码有一些错误不知道哪个是哪个。字符移动错误

using System.Collections; 
using System.Collections.Generic; 
using UnityEngine; 

public class FPSController : MonoBehaviour { 

    public float speed = 2f; 
    public float sensitivity = 2f; 
    public GameObject eyes; 
    public GameObject weapon; 
    CharacterController player; 

    private float verticalVelocity = 0; 
    private float gravity = 14f; 
    private float jumpForce = 10f; 

    private float rotX = 0; 
    private float rotY = 0; 


    // Use this for initialization 
    void Start() { 
     player = GetComponent<CharacterController>(); 

    } 

我估计错误从这里开始

 // Update is called once per frame 
    void Update() { 
     // //check if player is grounded 
     if (player.isGrounded){ 
      verticalVelocity = -gravity * Time.deltaTime; 
      //check if player has press space 
      if(Input.GetKeyDown(KeyCode.Space)) 
      { 
       verticalVelocity = jumpForce; 
      } 
     } 
     else 
     { 
      verticalVelocity -= gravity * Time.deltaTime; 
     } 
     Vector3 movement = Vector3.zero; 
     movement.x = Input.GetAxis("Horizontal") * speed; 
     movement.y = verticalVelocity; 
     movement.z = Input.GetAxis("Vertical") * speed; 
     rotX = Input.GetAxis("Mouse X") * sensitivity; 
     rotY = Input.GetAxis("Mouse Y") * sensitivity; 
     transform.Rotate(0,rotX,0); 
     eyes.transform.Rotate(-rotY,0,0); 
     weapon.transform.Rotate(rotY,0,0); 

     movement = transform.rotation * movement; 
     player.Move(movement*Time.deltaTime); 
    } 
} 
+1

你已经接受了一个“答案”,它实际上并没有解释什么是错的 - 它只是一般的诊断信息。有额外的诊断信息是好的,但这并不能真正帮助其他人面对同样的问题。 –

回答

0

它'小号现在的工作,读出真正帮助我发现有'没有错的代码,它'的只有我有冲突的值与字符控制器组件的属性之一是步骤偏移量。我只需要将偏移值更改为0.

1

尝试评论/删除此行verticalVelocity = -gravity * Time.deltaTime;我觉得你的刚体的地形机内部,可能使问题

+0

(OP正在使用CharacterContoroller,但答案可能仍然适用) – Maakep