當我按下前進/後退/左/右時,玩家角色確實移動但突然停止,但是當我按下空間跳躍時,它會再次開始移動我的代碼有一些錯誤不知道哪個是哪個。字符移動錯誤
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);
}
}
你已經接受了一個「答案」,它實際上並沒有解釋什麼是錯的 - 它只是一般的診斷信息。有額外的診斷信息是好的,但這並不能真正幫助其他人面對同樣的問題。 –