繁体   English   中英

统一播放器 controller 3d 没有正确跳跃

[英]player controller in unity 3d not jumping correctly

我正在制作一款带有重力切换机制的 3d 游戏。 我让玩家在 map 周围走动,它适用于所有角度,但跳跃是一团糟。 我想知道我怎样才能使跳跃工作。

跳转当前只是向本地向上向量添加一个值。 我不知道如何解决它。 我曾经让它工作过,但后来我不得不切换到物理运动而不是 static 运动,现在它不起作用。

这是代码:


void Update()
    {
        //think about moving
        isRunning = (Input.GetKey(KeyCode.LeftControl));
        isShifting = IntToBool((int)((BoolToInt(Input.GetKey(KeyCode.LeftAlt)))*BoolToInt(!isShiftable))+(BoolToInt(Input.GetKey(KeyCode.LeftShift))*BoolToInt(isShiftable)));

        Vector3 forward = transform.forward;//transform.TransformDirection(Vector3.forward);
        Vector3 right = transform.right;//transform.TransformDirection(Vector3.right);

        if (((isShifting && isRunning)||!(isShifting && isRunning))) {
            curSpeedX = walkingSpeed*Input.GetAxis("Vertical");
            curSpeedY = walkingSpeed*Input.GetAxis("Horizontal");
        }
        if (isShifting) {
            curSpeedX = ShiftSpeed*Input.GetAxis("Vertical");
            curSpeedY = ShiftSpeed*Input.GetAxis("Horizontal");
        }
        if (isRunning) {
            curSpeedX = runningSpeed*Input.GetAxis("Vertical");
            curSpeedY = runningSpeed*Input.GetAxis("Horizontal");
        }

        //jump
        if (!isGrounded) {
            ypoa -= gravity;
        }
        if (Input.GetKeyDown(KeyCode.Space)) {
            ypoa = jumpheight;
        }
        
        //actualy move
        moveDir = (ypoa*transform.up)+(curSpeedX*transform.forward)+(curSpeedY*transform.right);
        rb.velocity = (moveDir*speed);

        moveDir = new Vector3(0,0,0);
    }

    void OnCollisionEnter() {
        jumcount = 0;
        ypoa = 0;
        isGrounded = true;
    }

    void OnCollisionExit() {
        isGrounded = false;
    }

    void OnCollisionStay() {
        isGrounded = true;
    }

我正在使用统一 2019.4.28f 并且我在 windows 10

帮助表示赞赏。

尝试将您的物理代码移动到 FixedUpdate。 如果这不起作用,请您详细说明一下,例如当您按空格时会发生什么?

void Update()
    {
        //think about moving
        isRunning = (Input.GetKey(KeyCode.LeftControl));
        isShifting = IntToBool((int)((BoolToInt(Input.GetKey(KeyCode.LeftAlt)))*BoolToInt(!isShiftable))+(BoolToInt(Input.GetKey(KeyCode.LeftShift))*BoolToInt(isShiftable)));

        Vector3 forward = transform.forward;//transform.TransformDirection(Vector3.forward);
        Vector3 right = transform.right;//transform.TransformDirection(Vector3.right);

        if (((isShifting && isRunning)||!(isShifting && isRunning))) {
            curSpeedX = walkingSpeed*Input.GetAxis("Vertical");
            curSpeedY = walkingSpeed*Input.GetAxis("Horizontal");
        }
        if (isShifting) {
            curSpeedX = ShiftSpeed*Input.GetAxis("Vertical");
            curSpeedY = ShiftSpeed*Input.GetAxis("Horizontal");
        }
        if (isRunning) {
            curSpeedX = runningSpeed*Input.GetAxis("Vertical");
            curSpeedY = runningSpeed*Input.GetAxis("Horizontal");
        }

        //jump
        if (!isGrounded) {
            ypoa -= gravity;
        }
        if (Input.GetKeyDown(KeyCode.Space)) {
            ypoa = jumpheight;
        }
        
        //actualy move
        moveDir = (ypoa*transform.up)+(curSpeedX*transform.forward)+(curSpeedY*transform.right);
        rb.velocity = (moveDir*speed);

        moveDir = new Vector3(0,0,0);
    }

    void OnCollisionEnter() {
        jumcount = 0;
        ypoa = 0;
        isGrounded = true;
    }

    void OnCollisionExit() {
        isGrounded = false;
    }

    void OnCollisionStay() {
        isGrounded = true;
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM