Unity-带车轮碰撞器的转向

时间:2020-11-11 20:40:47

标签: c# unity3d wheelcollider

我正在制作“公路赛车”型游戏,并且正在使用车轮对撞机进行汽车运动。但是,我想以这样一种方式来操纵汽车:一旦松开“ A”或“ D”键,汽车便会按照路径直立,而不是像典型的高速公路那样基本上朝同一方向转向赛车游戏。

我试图通过释放“ A”和“ D”键时将所有旋转轴值都设为0来实现相同的目的。但是,这似乎并不自然,因为我想逐渐将汽车从当前方向转向路径的方向。另外,我的代码也使汽车的总体转向混乱,以至于其无法按预期运动。

这是我的代码:-

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

public class CarController : MonoBehaviour
{
    private float horizontalInput;
    private float currentSteerAngle;
    private float vel = 3f;

    [SerializeField] private float maxSteerAngle;

    [SerializeField] private WheelCollider frontLeftWheelCollider;
    [SerializeField] private WheelCollider frontRightWheelCollider;
    [SerializeField] private WheelCollider rearLeftWheelCollider;
    [SerializeField] private WheelCollider rearRightWheelCollider;

    [SerializeField] private Transform frontLeftWheelTransform;
    [SerializeField] private Transform frontRightWheelTransform;
    [SerializeField] private Transform rearLeftWheelTransform;
    [SerializeField] private Transform rearRightWheelTransform;

    private Rigidbody rb;
    private Transform tf;
    private ConstantForce cf;

    // Start is called before the first frame update
    void Start()
    {
        tf = GetComponent<Transform>();
        rb = GetComponent<Rigidbody>();
        cf = GetComponent<ConstantForce>();
    }

    // Update is called once per frame
    void Update()
    {
        GetInput();
        HandleSteering();
        CheckRotation();
        UpdateWheels();
    }

    private void GetInput()
    {
        horizontalInput = Input.GetAxis("Horizontal");
        if (!(Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.D)))
            StraightenCar();
    }

    private void HandleSteering()
    {
        currentSteerAngle = maxSteerAngle * horizontalInput * vel;
        frontLeftWheelCollider.steerAngle = currentSteerAngle;
        frontRightWheelCollider.steerAngle = currentSteerAngle;
    }

    private void UpdateWheels()
    {
        UpdateSingleWheel(frontLeftWheelCollider, frontLeftWheelTransform);
        UpdateSingleWheel(frontRightWheelCollider, frontLeftWheelTransform);
    }

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

    private void CheckRotation()
    {
        var rotation = tf.eulerAngles;
        rotation.y = ClampAngle(rotation.y, -30, 30);
        tf.eulerAngles = rotation;
    }

    private float ClampAngle(float angle, float min, float max)
    {
        if (angle < 90 || angle > 270)
        {
            if (angle > 180) angle -= 360;
            if (max > 180) max -= 360;
            if (min > 180) min -= 360;
        }
        angle = Mathf.Clamp(angle, min, max);
        if (angle < 0) angle += 360;
        return angle;
    }

    private void StraightenCar()
    {
        Vector3 rotation = tf.eulerAngles;
        rotation.x = 0;
        rotation.y = 0;
        rotation.z = 0;
        tf.eulerAngles = rotation;
    }
}

注意:总而言之,我需要以下方面的帮助:-

(1)左右移动后,将汽车拉直。

(2)使矫直过渡平滑。

非常感谢您。即使是最短的回复,我也非常感激。

0 个答案:

没有答案