盒子在地面上并前进时会旋转

问题描述 投票:0回答:1

我正在尝试创建一个可以跳跃、前进/后退和左/右的移动盒子。 但是,当这个盒子接触或接触到对撞机的某些东西时,它开始旋转并且不允许我移动它/不是很多,所以它开始绕圈(小圈)。

盒子有 3 个组件:
1 - 未设置为触发的盒子碰撞器
2 - 不使用重力的刚体(我在我的脚本中这样做)
3 - 我的移动玩家的脚本

这是我的自定义脚本:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[RequireComponent(typeof(Rigidbody))]
[RequireComponent(typeof(Collider))]

public class PlayerMovement : MonoBehaviour {
    public float moveSpeed = 9f;
    public float airMoveSpeed = 0.6f;
    public float jumpSpeed = 8f;
    public float gravity = 20f;
    public float groundDrag = 6f;
    public float airDrag = 4f;
    public Camera playerCamera;
    public float sensitivity = 2f;
    public float lookXLimit = 45f;
    public GroundCheck groundCheck;

    Vector3 moveDirection = Vector3.zero;
    Rigidbody rb;
    float rotationX = 0;
    bool isGrounded;

    [HideInInspector]
    public bool canMove = true;

    void Start()
    {
        rb = gameObject.GetComponent<Rigidbody>();

        isGrounded = false;

        // Lock cursor
        Cursor.lockState = CursorLockMode.Locked;
        Cursor.visible = false;
    }

    void Update()
    {
        CheckGround();
        MovePlayer();
        Gun();
        ControlDrag();
    }

    void CheckGround() {
        isGrounded = groundCheck.isGrounded;
    }

    void MovePlayer() {
        // Recalculate move direction based on axes
        Vector3 forward = transform.TransformDirection(Vector3.forward);
        Vector3 right = transform.TransformDirection(Vector3.right);
        float curSpeedX = canMove ? moveSpeed * Input.GetAxis("Vertical") : 0;
        float curSpeedY = canMove ? moveSpeed * Input.GetAxis("Horizontal") : 0;
        moveDirection = (forward * curSpeedX) + (right * curSpeedY);

        // moveDirection.y -= gravity * 1000;

        if (Input.GetButton("Jump") && canMove && isGrounded)
        {
            rb.AddForce(transform.up * jumpSpeed, ForceMode.Impulse);
        }

        // Move the player
        if(isGrounded) {
            // transform.position += moveDirection;
            rb.AddForce(moveDirection * Time.deltaTime, ForceMode.Acceleration);
        } else {
            // transform.position += moveDirection;
            rb.AddForce(moveDirection * Time.deltaTime * airMoveSpeed, ForceMode.Acceleration);
        }

        // Player and Camera rotation
        if (canMove)
        {
            rotationX += -Input.GetAxis("Mouse Y") * sensitivity;
            rotationX = Mathf.Clamp(rotationX, -lookXLimit, lookXLimit);
            playerCamera.transform.localRotation = Quaternion.Euler(rotationX, 0, 0);
            transform.rotation *= Quaternion.Euler(0, Input.GetAxis("Mouse X") * sensitivity, 0);
        }
    }

    void ControlDrag() {
        if(isGrounded) rb.drag = groundDrag;
        else rb.drag = airDrag;
    }
}
unity-game-engine game-physics collision rigid-bodies
1个回答
0
投票

首先,尝试将所有与物理相关的东西放入

FixedUpdate()
。这是 Unity 进行所有物理计算的地方。这很可能不是这里的原因,但通常建议这样做。

碰撞是“正面”发生还是没有全面接触?可能只是部分碰撞会对您的玩家运动产生扭矩,并且在您发布的代码中我没有看到任何控制角度运动的部分。

如果是这种情况,那么您可以尝试冻结绕 y 轴的旋转,就像您对其他两个轴所做的那样。

另一种选择是通过脚本控制立方体面向的方向。

© www.soinside.com 2019 - 2024. All rights reserved.