Unity3d 球员在团结中左移冲刺

Unity3d 球员在团结中左移冲刺,unity3d,move,shift,sprint,Unity3d,Move,Shift,Sprint,我的项目是第一人称跳跃和跑步,我希望我的球员通过按水平或垂直结合移位来冲刺 我已经做了一个新的输入,叫做Sprint,带有负按钮“左移” 我的球员做正常的移动,但他不冲刺 非常感谢 public class PlayerMovement : MonoBehaviour { public CharacterController controller; public float speed = 12f; public float sprint; public float gravity = -

我的项目是第一人称跳跃和跑步,我希望我的球员通过按水平或垂直结合移位来冲刺

我已经做了一个新的输入,叫做Sprint,带有负按钮“左移”

我的球员做正常的移动,但他不冲刺

非常感谢

public class PlayerMovement : MonoBehaviour
{

public CharacterController controller;

public float speed = 12f;
public float sprint;

public float gravity = -9.81f;
public float jumpHeight = 3f;

public Transform groundCheck;
public float groundDistance = 0.4f;
public LayerMask groundMask;

Vector3 velocity;
bool isGrounded;

// Update is called once per frame
void Update()
{

     

    


    isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);

    if(isGrounded && velocity.y < 0) {

        velocity.y = -2f;

    }

    float x = Input.GetAxis("Horizontal");
    float z = Input.GetAxis("Vertical");

    Vector3 move = transform.right * x + transform.forward * z;

    controller.Move(move * speed * Time.deltaTime);

    if (Input.GetButtonDown("Jump") && isGrounded)
    {

        velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);

    }

    velocity.y += gravity * Time.deltaTime;

    controller.Move(velocity * Time.deltaTime);

    // Noch nicht fertig -> Noch ausstehend
    if (Input.GetButtonDown("Horizontal") && Input.GetButtonDown("Sprint") || Input.GetButtonDown("Vertical") && Input.GetButtonDown("Sprint"))
    {
        controller.Move(move * (speed + sprint) * Time.deltaTime);

    }
}
公共类玩家移动:单一行为
{
公共字符控制器;
公共浮子速度=12f;
公众浮标冲刺;
公共浮子重力=-9.81f;
公共浮子跳线高度=3f;
公开审查;
公共浮地距离=0.4f;
公共层掩码地面掩码;
矢量3速度;
布尔被禁足;
//每帧调用一次更新
无效更新()
{
isground=physical.CheckSphere(groundCheck.position、groundDistance、groundMask);
如果(isGrounded&&velocity.y<0){
速度y=-2f;
}
float x=Input.GetAxis(“水平”);
float z=Input.GetAxis(“垂直”);
Vector3 move=transform.right*x+transform.forward*z;
控制器。移动(移动*速度*时间。延迟时间);
if(Input.GetButtonDown(“跳转”)和&isground)
{
速度y=数学Sqrt(跳跃高度*-2f*重力);
}
速度.y+=重力*时间增量;
控制器。移动(速度*时间。增量时间);
//不,不,不,不,不
if(Input.GetButtonDown(“水平”)和&Input.GetButtonDown(“冲刺”)| | Input.GetButtonDown(“垂直”)和&Input.GetButtonDown(“冲刺”))
{
控制器.Move(Move*(速度+冲刺)*时间.deltaTime);
}
}

处理输入的方式很可能存在问题。 我的假设是,当按下shift down键时,GetButtonDown仅在单个帧中返回true。请改用GetKey:

Input.GetKey(KeyCode.LeftShift)

如果这不起作用,请尝试以下两种方法:

Input.GetKeyDown(“左移”)

Input.GetKeyDown(KeyCode.LeftShift)

但这两个问题可能与“GetButtonDown”的问题相同


此外,我认为如果你使用英语注释而不是德语注释,会帮助人们更好地理解你的代码。我能够阅读,但很可能不会。不过,别担心,这也发生在我身上!

你的假设是正确的。GetKeyDown是按下键时(只调用一次)| GetKeyUp是松开键时(仅调用一次)|只要按住键,GetKey就会重复