2017-09-28 37 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