unity3d 我的3D赛车游戏有一些问题,如车轮转动y轴和汽车不去任何地方

zqry0prt  于 2023-01-02  发布在  其他
关注(0)|答案(1)|浏览(237)

我的车的轮胎旋转,但汽车不移动一英寸,车轮转向y轴,而不是左,右。这是我的代码,我添加了刚体和箱碰撞到我的车,以及可能是一个问题,导致汽车不动?(我确保把碰撞以上的车轮,以确保他们旋转。)

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

public class CarController : MonoBehaviour
{
    private float horizontalInput;
    private float verticalInput;
    private float steerAngle;
    private bool isBreaking;

    public WheelCollider FrontLeftCollider;
    public WheelCollider FrontRightCollider;
    public WheelCollider BackLeftCollider;
    public WheelCollider BackRightCollider;
    public Transform FrontLeftTransform;
    public Transform FrontRightTransform;
    public Transform BackLeftTransform;
    public Transform BackRightTransform;

    public float maxSteeringAngle = 30f;
    public float motorForce = 50f;
    public float brakeForce = 0f;

    private void FixedUpdate()
    {
        GetInput();
        HandleMotor();
        HandleSteering();
        UpdateWheels();
    }

    private void GetInput()
    {
        horizontalInput = Input.GetAxis("Horizontal");
        verticalInput = Input.GetAxis("Vertical");
        isBreaking = Input.GetKey(KeyCode.Space);
    }

    private void HandleSteering()
    {
        steerAngle = maxSteeringAngle * horizontalInput;
        FrontLeftCollider.steerAngle = steerAngle;
        FrontRightCollider.steerAngle = steerAngle;
    }

    private void HandleMotor()
    {
        FrontLeftCollider.motorTorque = verticalInput * motorForce;
        FrontRightCollider.motorTorque = verticalInput * motorForce;

        brakeForce = isBreaking ? 3000f : 0f;
        FrontLeftCollider.brakeTorque = brakeForce;
        FrontRightCollider.brakeTorque = brakeForce;
        BackLeftCollider.brakeTorque = brakeForce;
        BackRightCollider.brakeTorque = brakeForce;
    }

    private void UpdateWheels()
    {
        UpdateWheelPos(FrontLeftCollider, FrontLeftTransform);
        UpdateWheelPos(FrontRightCollider, FrontRightTransform);
        UpdateWheelPos(BackLeftCollider, BackLeftTransform);
        UpdateWheelPos(BackRightCollider, BackRightTransform);
    }

    private void UpdateWheelPos(WheelCollider wheelCollider, Transform trans)
    {
        Vector3 pos;
        Quaternion rot;
        wheelCollider.GetWorldPose(out pos, out rot);
        trans.rotation = rot;
        trans.position = pos;
    }

}

如果需要,我可以发送东西的截图,请不要害羞问。
我还没试过什么太害怕让它变得更糟的东西

c0vxltue

c0vxltue1#

一个刚体在被施加某种力之前是不会移动的,或者你使用它的MovePosition方法。这里有一个链接... https://docs.unity3d.com/ScriptReference/Rigidbody.MovePosition.html

相关问题