玩家的动作是不可思议的Input.GetaxisRaw
基本动作。我从这个youtube tutorial获得了当前的跳跃系统。在我自己键入代码之前,我一定要了解代码。
无论哪种方式,我实现的所有其他跳转方法都存在此问题。我认为这可能与使用W
和D
键给定的值有关。也许每个轴上的正值都覆盖了跳跃的能力?
这是我的Player
游戏对象中的相关代码。
从变量开始:
public float speed;
public float gravity;
public float jumpHeight = 3.0f;
public Transform groundCheck;
public float groundDistance = 0.4f;
public LayerMask groundMask;
bool isGrounded;
private CharacterController controller;
private Vector3 velocity;
private void Move() {
isGrounded = Physics.CheckSphere(groundCheck.position,groundDistance,groundMask);
if (isGrounded && velocity.y < 0) {
velocity.y = -2f;
}
float z = Input.GetaxisRaw("Vertical");
float x = Input.GetaxisRaw("Horizontal");
Vector3 move = transform.right * x + transform.forward * z;
controller.Move(move * speed * Time.deltaTime);
Jump();
}
private void Jump() {
if (Input.GetKeyDown(KeyCode.E) && isGrounded) {
velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
}
//This was my attempt at brute forcing the diagonal jump...didn't even run the Debug.Log().
if (Input.GetKeyDown(KeyCode.E) && Input.GetKey(KeyCode.W) && Input.GetKey(KeyCode.D) && isGrounded) {
Debug.Log("Moving Diagonal and attempting to jump");
velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
}
//=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
}
我知道互联网上有很多业余Unity问题,但是我还没有找到针对我特定问题的解决方案。当然,存在“一般情况下不能对角跳跃”问题,但没有“不能在一个特定方向上沿对角跳跃”问题。
如果您有任何想法,请告诉我。