简体   繁体   English

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

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

i am making a 3d game with a gravity switching mechanic.我正在制作一款带有重力切换机制的 3d 游戏。 i have the player walking around the map, and it works on all angles, however the jump is a mess.我让玩家在 map 周围走动,它适用于所有角度,但跳跃是一团糟。 i was wondering how i could make the jump work.我想知道我怎样才能使跳跃工作。

the jump currently just adds a value to the local up vector.跳转当前只是向本地向上向量添加一个值。 i have no idea how to fix it.我不知道如何解决它。 i had it working at one point but then i had to switch to physical movement instead of static movement and now it doesn't work.我曾经让它工作过,但后来我不得不切换到物理运动而不是 static 运动,现在它不起作用。

here is the code:这是代码:


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;
    }

i am using unity 2019.4.28f and am on windows 10我正在使用统一 2019.4.28f 并且我在 windows 10

help is appreciated.帮助表示赞赏。

Try to move your Physics code into FixedUpdate.尝试将您的物理代码移动到 FixedUpdate。 If this doesn't work, could you please elaborate more eg What happens when you press space?如果这不起作用,请您详细说明一下,例如当您按空格时会发生什么?

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