当我按下前进/后退/左/右时,玩家角色确实移动但突然停止,但是当我按下空间跳跃时,它会再次开始移动我的代码有一些错误不知道哪个是哪个。字符移动错误
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);
}
}
你已经接受了一个“答案”,它实际上并没有解释什么是错的 - 它只是一般的诊断信息。有额外的诊断信息是好的,但这并不能真正帮助其他人面对同样的问题。 –