我正在统一实施汽车控制器,这让我很头疼。有两个问题:
在谷歌和 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;
}
}
Unity中的轮子应该设计成如下结构:
Car
WheelCollider
WheelMesh (Mesh Filter & Mesh Renderer)
不要移动或旋转具有 WheelCollider 的对象,移动或旋转子节点以达到视觉效果。