其他分享
首页 > 其他分享> > Unity3D_WheelCollider车轮碰撞器

Unity3D_WheelCollider车轮碰撞器

作者:互联网

面板属性

代码方法

注意

   1. 不添加 Rigidbody 组件时,不会显示该碰撞器

   2. 车壳的 Collider 不能包裹住 WheelCollider,否则车不会移动

   3. 车轮碰撞器旋转时,车轮碰撞器所在游戏物体不会旋转,即 只有碰撞器旋转,车轮模型不会转,若想要车轮模型一起旋转需要根据 rpm 在代码中描述。

   4. 得到车轮每帧旋转的角度:float speed = rpm * 6 * Time.deltaTime

实例代码参考

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

public class Driving : MonoBehaviour
{
    // 四个车轮碰撞器(作为车壳的子物体)
    public WheelCollider wheel_Forword_Left;
    public WheelCollider wheel_Forword_Right;
    public WheelCollider wheel_Back_Left;
    public WheelCollider wheel_Back_Right;
    // 四个车轮模型(每个模型作为每个车轮碰撞器的子物体)
    private Transform wheelModel_Forword_Left;
    private Transform wheelModel_Forword_Right;
    private Transform wheelModel_Back_Left;
    private Transform wheelModel_Back_Right;
    // 重心(作为车壳的子物体)
    public Transform centerOfMass;

    void Start()
    {
        // 设置重心(注意是 localPosition ,Position会使车不规则跳动)
        GetComponent<Rigidbody>().centerOfMass = centerOfMass.localPosition;
        // 给车轮模型赋值
        wheelModel_Forword_Left = wheel_Forword_Left.transform.GetChild(0);
        wheelModel_Forword_Right = wheel_Forword_Right.transform.GetChild(0);
        wheelModel_Back_Left = wheel_Back_Left.transform.GetChild(0);
        wheelModel_Back_Right = wheel_Back_Right.transform.GetChild(0);
    }

    void FixedUpdate()
    {
        // 给前面两个轮子添加转弯驱动力
        // Input.GetAxisRaw("Horizontal") * 30f 表示轮子的最大旋转角度为30度
        wheel_Forword_Left.steerAngle = Input.GetAxisRaw("Horizontal") * 30f;
        wheel_Forword_Right.steerAngle = Input.GetAxisRaw("Horizontal") * 30f;
        // 给前面两个轮子添加前进驱动力
        wheel_Forword_Left.motorTorque = Input.GetAxisRaw("Vertical") * 10f;
        wheel_Forword_Right.motorTorque = Input.GetAxisRaw("Vertical") * 10f;

        // 车轮转动(不能写成wheelModel_Forword_Left.right,车轮的旋转方向会不确定)
        wheelModel_Forword_Left.Rotate(wheel_Forword_Left.rpm * 6 * Time.deltaTime * wheel_Forword_Left.transform.right, Space.World);
        wheelModel_Forword_Right.Rotate(wheel_Forword_Right.rpm * 6 * Time.deltaTime * wheel_Forword_Right.transform.right, Space.World);
        wheelModel_Back_Left.Rotate(wheel_Back_Left.rpm * 6 * Time.deltaTime * wheel_Back_Left.transform.right, Space.World);
        wheelModel_Back_Right.Rotate(wheel_Back_Right.rpm * 6 * Time.deltaTime * wheel_Back_Right.transform.right, Space.World);
        // 车轮转弯
        wheel_Forword_Left.transform.localEulerAngles = new Vector3(0, Input.GetAxisRaw("Horizontal") * 30f, 0);
        wheel_Forword_Right.transform.localEulerAngles = new Vector3(0, Input.GetAxisRaw("Horizontal") * 30f, 0);

        // 刹车
        if (Input.GetKeyDown(KeyCode.Space))
        {
            wheel_Forword_Left.brakeTorque = 100;
            wheel_Forword_Right.brakeTorque = 100;
            wheel_Back_Left.brakeTorque = 100;
            wheel_Back_Right.brakeTorque = 100;
        }else if (Input.GetKeyUp(KeyCode.Space))
        {
            wheel_Forword_Left.brakeTorque = 0;
            wheel_Forword_Right.brakeTorque = 0;
            wheel_Back_Left.brakeTorque = 0;
            wheel_Back_Right.brakeTorque = 0;
        }
    }
}

实例工程参考

基于Unity 2018.4.11f1(点击下载

 

标签:wheel,Unity3D,Right,WheelCollider,Back,Forword,车轮,Left
来源: https://www.cnblogs.com/kao-la-bao-bei/p/13032752.html