|
|
|
@ -147,6 +147,34 @@ public class VehicleController : MonoBehaviour
|
|
|
|
|
HandleMovement();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//private void ProcessInputs()
|
|
|
|
|
//{
|
|
|
|
|
// if (isAIControlled)
|
|
|
|
|
// {
|
|
|
|
|
// Vector3 targetPoint = targetTracker.TargetPos;
|
|
|
|
|
// targetPoint.y = transform.position.y;
|
|
|
|
|
|
|
|
|
|
// Vector3 directionToTarget = (targetPoint - transform.position).normalized;
|
|
|
|
|
// float angleToTarget = Vector3.Angle(transform.forward, directionToTarget);
|
|
|
|
|
// float distanceToTarget = Vector3.Distance(transform.position, targetPoint);
|
|
|
|
|
|
|
|
|
|
// if (distanceToTarget < targetStoppingDistance)
|
|
|
|
|
// inputVertical = 0f;
|
|
|
|
|
// else if (angleToTarget > 60f)
|
|
|
|
|
// inputVertical = 0.3f;
|
|
|
|
|
// else if (angleToTarget > 30f)
|
|
|
|
|
// inputVertical = 0.6f;
|
|
|
|
|
// else
|
|
|
|
|
// inputVertical = 0.95f;
|
|
|
|
|
|
|
|
|
|
// inputHorizontal = 0f;
|
|
|
|
|
// }
|
|
|
|
|
// else
|
|
|
|
|
// {
|
|
|
|
|
// inputVertical = CnInputManager.GetAxis("Vertical");
|
|
|
|
|
// inputHorizontal = CnInputManager.GetAxis("Horizontal");
|
|
|
|
|
// }
|
|
|
|
|
//}
|
|
|
|
|
private void ProcessInputs()
|
|
|
|
|
{
|
|
|
|
|
if (isAIControlled)
|
|
|
|
@ -171,8 +199,9 @@ public class VehicleController : MonoBehaviour
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
inputVertical = CnInputManager.GetAxis("Vertical");
|
|
|
|
|
inputHorizontal = CnInputManager.GetAxis("Horizontal");
|
|
|
|
|
// 👇 Dampen steering sensitivity
|
|
|
|
|
inputHorizontal = Mathf.Clamp(CnInputManager.GetAxis("Horizontal") * 0.85f, -1f, 1f);
|
|
|
|
|
inputVertical = Mathf.Clamp(CnInputManager.GetAxis("Vertical"), -1f, 1f);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -316,6 +345,7 @@ public class VehicleController : MonoBehaviour
|
|
|
|
|
{
|
|
|
|
|
velocityLocal = vehicleRigidbody.transform.InverseTransformDirection(vehicleRigidbody.velocity);
|
|
|
|
|
|
|
|
|
|
// Apply lateral friction
|
|
|
|
|
wheelFrictionMaterial.dynamicFriction = _frictionCurve.Evaluate(Mathf.Abs(velocityLocal.x / 100f));
|
|
|
|
|
steeringMultiplier = _turnCurve.Evaluate(velocityLocal.magnitude / maxSpeed);
|
|
|
|
|
|
|
|
|
@ -323,14 +353,30 @@ public class VehicleController : MonoBehaviour
|
|
|
|
|
{
|
|
|
|
|
steeringSign = Mathf.Sign(velocityLocal.z);
|
|
|
|
|
|
|
|
|
|
if (Mathf.Abs(accelerationInput) > 0.1f)
|
|
|
|
|
vehicleRigidbody.AddTorque(Vector3.up * (steeringAI * steeringSign * steeringSensitivity * 100f * steeringMultiplier /** steeringFactor*/));
|
|
|
|
|
// Steering torque
|
|
|
|
|
if (inputVertical > 0.1f || velocityLocal.z > 1f)
|
|
|
|
|
vehicleRigidbody.AddTorque(Vector3.up * (steeringAI * steeringSign * steeringSensitivity * 100f * steeringMultiplier));
|
|
|
|
|
else if (inputVertical < -0.1f || velocityLocal.z < -1f)
|
|
|
|
|
vehicleRigidbody.AddTorque(Vector3.up * (steeringAI * steeringSign * steeringSensitivity * 100f * steeringMultiplier));
|
|
|
|
|
|
|
|
|
|
if (Mathf.Abs(accelerationInput) > 0.1f)
|
|
|
|
|
sphereRigidbody.velocity = Vector3.Lerp(sphereRigidbody.velocity, vehicleRigidbody.transform.forward * (accelerationInput * maxSpeed), accelerationForce / 10f * Time.fixedDeltaTime);
|
|
|
|
|
// Brake logic (if needed)
|
|
|
|
|
// You can add: sphereRigidbody.constraints = brakeInput > 0.1f ? RigidbodyConstraints.FreezeRotationX : RigidbodyConstraints.None;
|
|
|
|
|
|
|
|
|
|
// Velocity alignment using Lerp (matches reference)
|
|
|
|
|
if (Mathf.Abs(inputVertical) > 0.1f && brakeInput < 0.1f)
|
|
|
|
|
{
|
|
|
|
|
Vector3 targetVelocity = vehicleRigidbody.transform.forward * (inputVertical * maxSpeed);
|
|
|
|
|
sphereRigidbody.velocity = Vector3.Lerp(
|
|
|
|
|
sphereRigidbody.velocity,
|
|
|
|
|
targetVelocity,
|
|
|
|
|
accelerationForce / 10f * Time.fixedDeltaTime
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Downforce for grip
|
|
|
|
|
sphereRigidbody.AddForce(-transform.up * (downforce * sphereRigidbody.mass));
|
|
|
|
|
|
|
|
|
|
// Align car with surface
|
|
|
|
|
vehicleRigidbody.MoveRotation(
|
|
|
|
|
Quaternion.Slerp(
|
|
|
|
|
vehicleRigidbody.rotation,
|
|
|
|
@ -341,17 +387,61 @@ public class VehicleController : MonoBehaviour
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// Optional: allow air control
|
|
|
|
|
if (allowAirControl)
|
|
|
|
|
vehicleRigidbody.AddTorque(Vector3.up * (steeringAI * steeringSensitivity * 100f * steeringMultiplier /** steeringFactor*/));
|
|
|
|
|
{
|
|
|
|
|
vehicleRigidbody.AddTorque(Vector3.up * (steeringAI * steeringSensitivity * 100f * steeringMultiplier));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Apply velocity while airborne
|
|
|
|
|
sphereRigidbody.velocity = Vector3.Lerp(
|
|
|
|
|
sphereRigidbody.velocity,
|
|
|
|
|
(vehicleRigidbody.transform.forward * (accelerationInput * maxSpeed)) + Vector3.down * (gravityForce * 9.8f),
|
|
|
|
|
(accelerationForce / 25f) * Time.deltaTime
|
|
|
|
|
vehicleRigidbody.transform.forward * (inputVertical * maxSpeed) + Vector3.down * gravityForce * 9.8f,
|
|
|
|
|
accelerationForce / 25f * Time.deltaTime
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//private void HandleMovement()
|
|
|
|
|
|
|
|
|
|
//private void HandleMovement() //second
|
|
|
|
|
//{
|
|
|
|
|
// velocityLocal = vehicleRigidbody.transform.InverseTransformDirection(vehicleRigidbody.velocity);
|
|
|
|
|
|
|
|
|
|
// wheelFrictionMaterial.dynamicFriction = _frictionCurve.Evaluate(Mathf.Abs(velocityLocal.x / 100f));
|
|
|
|
|
// steeringMultiplier = _turnCurve.Evaluate(velocityLocal.magnitude / maxSpeed);
|
|
|
|
|
|
|
|
|
|
// if (Grounded)
|
|
|
|
|
// {
|
|
|
|
|
// steeringSign = Mathf.Sign(velocityLocal.z);
|
|
|
|
|
|
|
|
|
|
// if (Mathf.Abs(accelerationInput) > 0.1f)
|
|
|
|
|
// vehicleRigidbody.AddTorque(Vector3.up * (steeringAI * steeringSign * steeringSensitivity * 100f * steeringMultiplier /** steeringFactor*/));
|
|
|
|
|
|
|
|
|
|
// if (Mathf.Abs(accelerationInput) > 0.1f)
|
|
|
|
|
// sphereRigidbody.velocity = Vector3.Lerp(sphereRigidbody.velocity, vehicleRigidbody.transform.forward * (accelerationInput * maxSpeed), accelerationForce / 10f * Time.fixedDeltaTime);
|
|
|
|
|
|
|
|
|
|
// sphereRigidbody.AddForce(-transform.up * (downforce * sphereRigidbody.mass));
|
|
|
|
|
|
|
|
|
|
// vehicleRigidbody.MoveRotation(
|
|
|
|
|
// Quaternion.Slerp(
|
|
|
|
|
// vehicleRigidbody.rotation,
|
|
|
|
|
// Quaternion.FromToRotation(vehicleRigidbody.transform.up, surfaceHit.normal) * vehicleRigidbody.transform.rotation,
|
|
|
|
|
// 0.12f
|
|
|
|
|
// )
|
|
|
|
|
// );
|
|
|
|
|
// }
|
|
|
|
|
// else
|
|
|
|
|
// {
|
|
|
|
|
// if (allowAirControl)
|
|
|
|
|
// vehicleRigidbody.AddTorque(Vector3.up * (steeringAI * steeringSensitivity * 100f * steeringMultiplier /** steeringFactor*/));
|
|
|
|
|
|
|
|
|
|
// sphereRigidbody.velocity = Vector3.Lerp(
|
|
|
|
|
// sphereRigidbody.velocity,
|
|
|
|
|
// (vehicleRigidbody.transform.forward * (accelerationInput * maxSpeed)) + Vector3.down * (gravityForce * 9.8f),
|
|
|
|
|
// (accelerationForce / 25f) * Time.deltaTime
|
|
|
|
|
// );
|
|
|
|
|
// }
|
|
|
|
|
//}
|
|
|
|
|
//private void HandleMovement() //original
|
|
|
|
|
//{
|
|
|
|
|
// velocityLocal = vehicleRigidbody.transform.InverseTransformDirection(vehicleRigidbody.velocity);
|
|
|
|
|
|
|
|
|
|