我正在编写一个汽车游戏,一切都很顺利,但在某些时候我不知道为什么汽车会缓慢下落,尽管重力应该是正常的 这是两个可能有错误的脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class wheelController : MonoBehaviour
{
public AnimationCurve potenza;
public AnimationCurve motoCurve;
public AnimationCurve frontTiresCurve;
public AnimationCurve backTiresCurve;
public Rigidbody carRb;
public Transform tire;
public Transform carTransform;
public float maxSteerAngle;
public float offset;
public float suspensionRestDistance;
public LayerMask Ground;
public RaycastHit tirehit;
public float strenght;
public float Damper;
public float Grip;
public float tireMass;
[SerializeField]
private float inputDir;
private float HinputDir;
public float maxSpeed;
public float speed;
public bool isFrontWheel;
// Start is called before the first frame update
void Start()
{
carRb = carTransform.GetComponent<Rigidbody>();
tire = this.transform;
}
// Update is called once per frame
void FixedUpdate()
{
if(carRb.velocity.magnitude< 0.4f)
{
carRb.velocity = Vector3.zero;
}
inputDir = Input.GetAxis("Vertical");
HinputDir = Input.GetAxis("Horizontal");
//FLW Math
Ray tireray = new Ray(tire.position, -transform.up);
if (Physics.Raycast(tireray, out tirehit, suspensionRestDistance , Ground))
{
//Movement
Vector3 accelDirection = tire.forward;
Vector3 tireWorldVel = carRb.GetPointVelocity(tire.position);
Vector3 steeringDirecttion = tire.right;
float steeringVel = Vector3.Dot(steeringDirecttion, tireWorldVel);
float bckwrdsVel = -Vector3.Dot(tire.forward, tireWorldVel);
float fwdVel = Vector3.Dot(tire.forward, tireWorldVel);
if (isFrontWheel == true)
{
float gripWCruve;
if (Input.GetButton("Drift"))
{
gripWCruve = .2f;
maxSteerAngle = 40f;
}
else
{
float velOnRed = Vector3.Dot(tire.right, tireWorldVel);
gripWCruve = frontTiresCurve.Evaluate(Mathf.Clamp01(Mathf.Abs(carRb.velocity.magnitude) / Mathf.Abs(velOnRed)));
maxSteerAngle = 20f;
//Debug.Log(velOnRed);
}
if (inputDir != 0.0f)
{
float carSpeed = Vector3.Dot(carTransform.forward, carRb.velocity);
float normalizedSpeed = Mathf.Clamp01(Mathf.Abs(carSpeed) / maxSpeed);
float availableTorque = motoCurve.Evaluate(normalizedSpeed) * inputDir;
carRb.AddForceAtPosition(tire.forward * availableTorque * speed, tire.position);
Debug.DrawRay(tire.position, tire.forward * availableTorque * speed, Color.blue);
}
//steering
tire.localRotation = Quaternion.Euler(new Vector3(carTransform.rotation.x, carTransform.rotation.y + maxSteerAngle * Input.GetAxis("Horizontal"), carTransform.rotation.z));
float desiredVelChange = -steeringVel * gripWCruve;
float desiredAccelleration = desiredVelChange / Time.fixedDeltaTime;
carRb.AddForceAtPosition(steeringDirecttion * tireMass * desiredAccelleration, tire.position);
Debug.DrawRay(tire.position, tire.right * 10, Color.red);
}
if (isFrontWheel != true)
{
float gripWCruve;
if (Input.GetButton("Drift"))
{
gripWCruve = 0.2f;
}else
{
float velOnRed = Vector3.Dot(tire.right, tireWorldVel);
gripWCruve = backTiresCurve.Evaluate(Mathf.Clamp01(Mathf.Abs(carRb.velocity.magnitude) /Mathf.Abs( velOnRed)));
}
//Debug.Log(steeringVel);
float desiredVelChange = -steeringVel * gripWCruve;
float desiredAccelleration = desiredVelChange / Time.fixedDeltaTime;
carRb.AddForceAtPosition(steeringDirecttion * tireMass * desiredAccelleration, tire.position);
}
if (inputDir == 0)
{
//friction
float VelGrip = -Vector3.Dot(tire.forward, tireWorldVel);
float VelChange = VelGrip * Grip;
carRb.AddForceAtPosition(tire.forward * VelChange, tire.position);
if(Mathf.Abs(carRb.velocity.z) <= 0.35f )
carRb.velocity = Vector3.zero;
Debug.DrawRay(tire.position, tire.forward * VelChange, Color.blue);
}
//tiene la macchina su
float carSpeedo = Vector3.Dot(carTransform.forward, carRb.velocity);
float normalizedSpeedo = Mathf.Clamp01(Mathf.Abs(carSpeedo) / maxSpeed);
Vector3 springDirection = tire.transform.up;
float offset = suspensionRestDistance - tirehit.distance;
float vel = Vector3.Dot(springDirection, tireWorldVel);
float strenghtWcurve = potenza.Evaluate(normalizedSpeedo);
float force = (offset * strenght) - (vel * Damper );
carRb.AddForceAtPosition(springDirection * force,tire.position);
}
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CarController : MonoBehaviour
{
private float horizontalInput, verticalInput;
private float currentSteerAngle, currentbreakForce;
private bool isBreaking;
// Settings
[SerializeField] private float motorForce, breakForce, maxSteerAngle;
// Wheel Colliders
[SerializeField] private WheelCollider frontLeftWheelCollider, frontRightWheelCollider;
[SerializeField] private WheelCollider rearLeftWheelCollider, rearRightWheelCollider;
// Wheels
[SerializeField] private Transform frontLeftWheelTransform, frontRightWheelTransform;
[SerializeField] private Transform rearLeftWheelTransform, rearRightWheelTransform;
private void FixedUpdate()
{
GetInput();
HandleMotor();
HandleSteering();
UpdateWheels();
}
private void GetInput()
{
// Steering Input
horizontalInput = Input.GetAxis("Horizontal");
// Acceleration Input
verticalInput = Input.GetAxis("Vertical");
// Breaking Input
isBreaking = Input.GetKey(KeyCode.Space);
}
private void HandleMotor()
{
frontLeftWheelCollider.motorTorque = verticalInput * motorForce;
frontRightWheelCollider.motorTorque = verticalInput * motorForce;
currentbreakForce = isBreaking ? breakForce : 0f;
ApplyBreaking();
}
private void ApplyBreaking()
{
frontRightWheelCollider.brakeTorque = currentbreakForce;
frontLeftWheelCollider.brakeTorque = currentbreakForce;
rearLeftWheelCollider.brakeTorque = currentbreakForce;
rearRightWheelCollider.brakeTorque = currentbreakForce;
}
private void HandleSteering()
{
currentSteerAngle = maxSteerAngle * horizontalInput;
frontLeftWheelCollider.steerAngle = currentSteerAngle;
frontRightWheelCollider.steerAngle = currentSteerAngle;
}
private void UpdateWheels()
{
UpdateSingleWheel(frontLeftWheelCollider, frontLeftWheelTransform);
UpdateSingleWheel(frontRightWheelCollider, frontRightWheelTransform);
UpdateSingleWheel(rearRightWheelCollider, rearRightWheelTransform);
UpdateSingleWheel(rearLeftWheelCollider, rearLeftWheelTransform);
}
private void UpdateSingleWheel(WheelCollider wheelCollider, Transform wheelTransform)
{
Vector3 pos;
Quaternion rot;
wheelCollider.GetWorldPose(out pos, out rot);
wheelTransform.rotation = rot;
wheelTransform.position = pos;
}
}
我也使用cinemachine作为相机,但我不认为这是一个问题
我尝试更改一些变量,但没有任何改变
不要说“可能有错误”,而是告诉我们它所存在的错误,以便我们可以帮助您,并且您是否使用刚体来实现重力?如果您回答这些问题,我可以提供更多帮助。