unity3d 为什么汽车车轮旋转奇怪,并脱落的汽车在一个整体?更新的问题

qvtsj1bj  于 2023-03-19  发布在  其他
关注(0)|答案(1)|浏览(170)

我正在统一实现一个汽车控制器,这让我很头疼。有两个问题:
1.在播放模式下,前轮在z轴上旋转90度。
第一节第一节第一节第一节第一次
1.车轮刚从车上掉下来。
以下是汽车模型的层次结构:

在谷歌和YouTube上搜索,但没有找到任何相关的.修改代码多次,但同样的错误结果.请帮我.这里是整个代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class playerMovement : MonoBehaviour
{
  [SerializeField] WheelCollider leftFront;
  [SerializeField] WheelCollider rightFront;

  public Transform leftFrontWheelTransform;
  public Transform rightFrontWheelTransform;

  public GameObject player;
  public Transform toBeRolled;

  private float maxTurnAngle = 30f;
  private float currentTurnAngle = 30f;
    
  public static float currentSpeed;
  public static float DriveSpeed = 0.0f;
  public static float slowSpeed;
  public float steerAmount = 120f;
  private float steer;

  private float timer = 0f;
  private float SecondCount = 1f; //Delay time

    void Update()
      {
        slowSpeed = DriveSpeed * 0.7f; //slowSpeed = 5(initial) * 0.7 => 3.5 initially.
        currentSpeed = DriveSpeed;  //5
        if(Input.GetKey(KeyCode.Space))
        {
            currentSpeed = slowSpeed;
        }

        timer += Time.deltaTime;
        player.transform.position += player.transform.forward * (currentSpeed) * Time.deltaTime;

        float _horizontal = Input.GetAxis("Horizontal");
        steer += _horizontal * steerAmount * Time.deltaTime;

        float roll = Mathf.Lerp(0, 0, Mathf.Abs(_horizontal)) * -Mathf.Sign(_horizontal);
        float rollSTEAR = Mathf.Lerp(0, 10, Mathf.Abs(_horizontal)) * -Mathf.Sign(_horizontal);

        transform.localRotation = Quaternion.Euler(Vector3.up * steer + Vector3.forward * roll);
        toBeRolled.localRotation = Quaternion.Euler(Vector3.forward * rollSTEAR);

        currentTurnAngle = maxTurnAngle * _horizontal;
        leftFront.steerAngle = currentTurnAngle;    
        rightFront.steerAngle = currentTurnAngle;

        UpdateWheel(leftFront , leftFrontWheelTransform);
        UpdateWheel(rightFront , rightFrontWheelTransform);

         if(timer > SecondCount)
         {
             DriveSpeed += 0.00001f;
             //speed up text & sound on screen
             timer = 0f;
         }

     }
    
    void UpdateWheel(WheelCollider col, Transform trans)
    {
      Vector3 position;
      Quaternion rotation;

      col.GetWorldPose(out position, out rotation);

      trans.position = position;
      trans.rotation = rotation;

    }
}

更新:好吧,我做了更多的研究,更新了代码,现在一切正常,但我很好奇,如果我做的是最佳的方式或只是一个蛮力:

更新:
1.将汽车层次结构更改为:

1.更新代码:

using JetBrains.Annotations;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.Rendering.Universal;

 public class CarMovement : MonoBehaviour
 {
 public WheelCollider leftFront;
 public WheelCollider rightFront;
 public WheelCollider leftRear;
 public WheelCollider rightRear;

 public Rigidbody car;

 public Transform LF_wheel;
 public Transform RF_wheel;
 public Transform LR_wheel;
 public Transform RR_wheel;

 public float maxTorq;
 public float maxSteerAngle;

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

 void Update()
 {
     takeingInput();
     steer();
     updateWheels();
 }

 public void takeingInput()
 {
    float _vertical = Input.GetAxis("Vertical");

     //leftRear.motorTorque = _vertical * maxTorq;
     //rightRear.motorTorque = _vertical * maxTorq;

leftRear.motorTorque = Mathf.Clamp(_vertical, -1.0f, 1.0f) * maxTorq;
rightRear.motorTorque = Mathf.Clamp(_vertical, -1.0f, 1.0f) * maxTorq;

}

public void steer()
 {
     float _horizontal = Input.GetAxis("Horizontal");
     leftFront.steerAngle = Mathf.Clamp(_horizontal, -1.0f, 1.0f) * maxSteerAngle;
     rightFront.steerAngle = Mathf.Clamp(_horizontal, -1.0f, 1.0f) * maxSteerAngle;

     LF_wheel.localRotation = Quaternion.Euler(0,leftFront.steerAngle,90);
     RF_wheel.localRotation = Quaternion.Euler(0,rightFront.steerAngle,90);
 }
 public void updateWheels()
 {
     wheelUpdate(leftFront, LF_wheel);
     wheelUpdate(rightFront, RF_wheel);
     wheelUpdate(leftRear, LR_wheel);
     wheelUpdate(rightRear, RR_wheel);

     wheelUpdateFix(LR_wheel);
     wheelUpdateFix(RR_wheel);

 }

 public void wheelUpdate(WheelCollider col, Transform trans)
 {
     Vector3 pos; Quaternion rot;

     col.GetWorldPose(out pos, out rot);

     trans.position = pos;
     //trans.rotation = rot;

 }
 public void wheelUpdateFix(Transform trans)
 {
     Quaternion _fixeRot = transform.rotation;
     _fixeRot = _fixeRot * Quaternion.Euler(0, 0, -90);
     trans.rotation = _fixeRot;

 }

}

cnjp1d6j

cnjp1d6j1#

Unity中的车轮应设计为以下结构:

Car
 ├ LF collider
 │  └ LF transform (Mesh Filter & Mesh Renderer)
 ├ RF collider
 │  └ RF transform
 ├ LR collider
 │  └ LR transform
 └ LR collider
    └ RR transform

不要移动或旋转具有WheelCollider的对象,移动或旋转子节点以获得视觉效果。
如果轴不沿着X轴,则需要附加变换以不断旋转车轮。

Car
 └ LF collider
    └ LF transform
      └ Wheel (Put rotated model here)

相关问题