当我将脚本附加到对象时,为什么 Unity 的重力会破坏?

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

我正在Unity中制作一个卡丁车赛车,并开发了玩家控制的脚本。由于某种原因,当我将此脚本附加到游戏对象时,该对象的下落速度比正常情况下慢 100 倍。我一生都无法弄清楚为什么。这是播放器控制脚本,有什么想法吗?

using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour
{
    public float moveSpeed = 10f;           // Movement speed of the player
    public float rotationSpeed = 100f;      // Rotation speed of the player
    public float maxSpeed = 20f;            // Maximum speed the player can move
    public float acceleration = 5f;         // Acceleration of the player
    public float braking = 10f;             // Braking force applied to slow down the player
    public float reverseSpeed = 5f;         // Speed of reverse movement

    private Rigidbody rb;                   // Reference to the player's Rigidbody component
    private float currentSpeed = 0f;        // Current speed of the player

    private float moveInput;
    private float rotationInput;

    // New variables for drifting
   public float driftTraction = 0.5f;
   public float driftForce = 10f;
   private bool isDrifting = false;
   private bool isTurningLeft = false;
   private bool isTurningRight = false;
   private bool leftTrueRightFalse;
   private float lockedRotationInput;

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

    void Update()
    {
            // Get input for movement and rotation
            moveInput = Input.GetAxis("Vertical");
            rotationInput = Input.GetAxis("Horizontal");

            isTurningLeft = rotationInput < 0f;
            isTurningRight = rotationInput > 0f;

            if (Input.GetKeyDown(KeyCode.LeftShift))
              {
                  StartDrift();
              }
              else if (Input.GetKeyUp(KeyCode.LeftShift))
              {
                  EndDrift();
              }
    }

    void FixedUpdate()
    {

        // Calculate movement
        float moveAmount = 0f;

        // Accelerate if moving forward, reverse if moving backward, or brake if not moving
        if (moveInput > 0 && currentSpeed >= 0)
        {
            moveAmount = acceleration * Time.deltaTime;
        }
        else if (moveInput > 0 && currentSpeed <= 0)
        {
            moveAmount = braking * Time.deltaTime;
        }
        else if (moveInput < 0 && currentSpeed >= 0)
        {
            moveAmount = -braking * Time.deltaTime;
        }
        else if (moveInput < 0 && currentSpeed < 0)
        {
            moveAmount = -acceleration * Time.deltaTime;
        }
        else if (moveInput < 0 && currentSpeed == 0)
        {
            moveAmount = -reverseSpeed * Time.deltaTime;
        }
        else if (currentSpeed < 0.01 && currentSpeed > -0.01){
          moveAmount = 0;
        }
        else if (currentSpeed > 0)
        {
          moveAmount = 0.5f * -braking * Time.deltaTime;
        }
        else if (currentSpeed < 0)
        {
          moveAmount = 0.5f * braking * Time.deltaTime;
        }

        if (isDrifting){
          if ((isTurningLeft && !leftTrueRightFalse) || (isTurningRight && leftTrueRightFalse)){
            // Apply rotation only if drift direction matches
            float targetRotation = rotationInput * rotationSpeed * 0.8f * Time.deltaTime;
            Quaternion targetRotationQuaternion = Quaternion.Euler(0f, targetRotation, 0f);
            transform.rotation = Quaternion.Lerp(transform.rotation, transform.rotation * targetRotationQuaternion, Time.deltaTime * rotationSpeed);
          } else {
            // Slight rotation
            float targetRotation = rotationInput * rotationSpeed * 0.4f * Time.deltaTime;
            Quaternion targetRotationQuaternion = Quaternion.Euler(0f, targetRotation, 0f);
            transform.rotation = Quaternion.Lerp(transform.rotation, transform.rotation * targetRotationQuaternion, Time.deltaTime * rotationSpeed);
          }
        } else {
          // Apply rotation
          float targetRotation = rotationInput * rotationSpeed * Time.deltaTime;
          Quaternion targetRotationQuaternion = Quaternion.Euler(0f, targetRotation, 0f);
          transform.rotation = Quaternion.Lerp(transform.rotation, transform.rotation * targetRotationQuaternion, Time.deltaTime * rotationSpeed);
        }

        // Apply acceleration or braking to change speed
        currentSpeed += moveAmount;
        currentSpeed = Mathf.Clamp(currentSpeed, -reverseSpeed, maxSpeed);

        // Apply forward or reverse movement based on speed
        rb.velocity = transform.forward * currentSpeed;

        // Apply drifting physics if drifting
        if (isDrifting)
        {
            ApplyDriftPhysics();
        }
    }

    private void StartDrift()
   {
       if (!isDrifting){
         if (isTurningLeft){
           leftTrueRightFalse = false;
           isDrifting = true;
           // Add visual and audio feedback for starting a drift
           lockedRotationInput = rotationInput;
         } else if (isTurningRight){
           leftTrueRightFalse = true;
           isDrifting = true;
           // Add visual and audio feedback for starting a drift
           lockedRotationInput = rotationInput;
         } else {
           // Nothing
         }
       }
   }

   private void EndDrift()
   {
       isDrifting = false;
       // Reset kart physics to normal
       rb.drag = 0f;
   }

    private void ApplyDriftPhysics()
    {
        // Reduce traction to simulate drifting
        rb.drag = driftTraction;

        // Apply sideways force to induce drift
        if (!leftTrueRightFalse){
          Vector3 driftForceDirection = transform.right;
          rb.AddForce(driftForceDirection * driftForce, ForceMode.Acceleration);
        } else {
          Vector3 driftForceDirection = -transform.right;
          rb.AddForce(driftForceDirection * driftForce, ForceMode.Acceleration);
        }

        // Apply rotation
        float targetRotation = lockedRotationInput * rotationSpeed * 0.6f * Time.deltaTime;
        Quaternion targetRotationQuaternion = Quaternion.Euler(0f, targetRotation, 0f);
        transform.rotation = Quaternion.Lerp(transform.rotation, transform.rotation * targetRotationQuaternion, Time.deltaTime * rotationSpeed);
    }

    public void ResetSpeedAndAcceleration()
   {
       currentSpeed = 0f; // Reset current speed to 0
       moveInput = 0f; // Reset move input
       rotationInput = 0f; // Reset rotation input
   }

    public int getCurrentSpeed(){
      if (currentSpeed >= 0)
      {
        return 1;
      }
      else
      {
        return 0;
      }
    }
}

我在脚本中没有看到任何会影响重力作用在物体上的方式。

c# unity-game-engine game-physics
1个回答
0
投票

我不太确定,但我认为你的问题是你覆盖了 y 轴速度。 改变这个:

rb.velocity = transform.forward * currentSpeed;

这样:

Vector3 newVelocity = transform.forward * currentSpeed;
newVelocity.y = rb.velocity.y;
rb.velocity = newVelocity;
© www.soinside.com 2019 - 2024. All rights reserved.