コード例 #1
0
    void Update()
    {
        playerForward = playerForwardObject.transform.position - playerBody.transform.position;
        playerForward = playerForward.normalized;
        playerRight   = playerRightObject.transform.position - playerBody.transform.position;
        playerRight   = playerRight.normalized;
        playerUp      = playerUpObject.transform.position - playerBody.transform.position;
        playerUp      = playerUp.normalized;

        CheckRotation(!playerGravity.isRotating);

        if (debugStuff.debugPlayerMovement)
        {
            Debug.DrawRay(playerBody.transform.position, playerForward * 10, Color.blue);
            Debug.DrawRay(playerBody.transform.position, playerUp * 10, Color.green);
            Debug.DrawRay(playerBody.transform.position, playerRight * 10, Color.red);
        }

        if (isGrounded && !jumping)
        {
            if (Input.GetKey(KeyCode.LeftShift))
            {
                if (!running)
                {
                    running = true;
                }
            }
            else
            {
                if (running)
                {
                    running = false;
                }
            }

            if (Input.GetKey(KeyCode.W))
            {
                if (running)
                {
                    rigbod.velocity = playerForward * playerStats.MovmentSpeed * playerStats.RunMultiplier;
                }
                else
                {
                    rigbod.velocity = playerForward * playerStats.MovmentSpeed;
                }
                moving = true;
            }

            if (Input.GetKey(KeyCode.A))
            {
                if (running)
                {
                    rigbod.velocity = -playerRight * playerStats.MovmentSpeed * playerStats.RunMultiplier;
                }
                else
                {
                    rigbod.velocity = -playerRight * playerStats.MovmentSpeed;
                }
                moving = true;
            }

            if (Input.GetKey(KeyCode.S))
            {
                if (running)
                {
                    rigbod.velocity = -playerForward * playerStats.MovmentSpeed * playerStats.RunMultiplier;
                }
                else
                {
                    rigbod.velocity = -playerForward * playerStats.MovmentSpeed;
                }
                moving = true;
            }

            if (Input.GetKey(KeyCode.D))
            {
                if (running)
                {
                    rigbod.velocity = playerRight * playerStats.MovmentSpeed * playerStats.RunMultiplier;
                }
                else
                {
                    rigbod.velocity = playerRight * playerStats.MovmentSpeed;
                }
                moving = true;
            }

            if (Input.GetKeyDown(KeyCode.Space) && !isRamp)
            {
                rigbod.AddForce(-gravity.GravityDirection * playerStats.JumpStrength);
                jumping = true;
                moving  = true;
            }

            if (!moving && !jumping && rigbod.velocity.magnitude > 0.001)
            {
                rigbod.velocity -= rigbod.velocity * 0.9f;
                if (debugStuff.debugPlayerMovement)
                {
                    Debug.Log("slowing player: PlayerMovment.cs");
                }
            }
        }

        if (Input.GetKeyDown(KeyCode.LeftAlt) && !playerGravity.isRotating)
        {
            playerGravity.isRotating = true;
            playerMouseLook.ChangeGravity();
            rigbod.AddForce(gravity.GetPreviousFallDirectionVector() * playerStats.JumpStrength);
            isUpRight = false;
        }

        if (moving)
        {
            moving = false;
        }

        Vector3 f = playerBodyShell.transform.rotation.eulerAngles;
        Vector3 u = Quaternion.LookRotation(playerForward, -gravity.GravityDirection).eulerAngles;

        if (isGrounded && !isUpRight)
        {
            if (debugStuff.debugPlayerMovement)
            {
                Debug.Log("grounded and not upright");
            }

            float   dot        = Mathf.Abs(Vector3.Dot(-gravity.GravityDirection, playerForward) / -gravity.GravityDirection.magnitude);
            Vector3 newPos     = playerForwardObject.transform.position + (-gravity.GravityDirection * dot);
            Vector3 newForward = newPos - playerBodyShell.transform.position;
            newForward = newForward.normalized;

            Quaternion dirQ  = Quaternion.LookRotation(newForward, -gravity.GravityDirection);                                     // what direction i want
            Quaternion slerp = Quaternion.Slerp(playerBodyShell.transform.rotation, dirQ, gravity.RotationSpeed * Time.deltaTime); // rotates to it over time


            if (dot > 0.001)
            {
                playerBodyShell.transform.rotation = slerp;
            }
            else
            {
                isUpRight = true;
            }
        }
    }