32 lines
1.1 KiB
C#
32 lines
1.1 KiB
C#
using UnityEngine;
|
|
using UnityEngine.AI;
|
|
|
|
namespace Code.Example.Race.Racer.Car
|
|
{
|
|
public class CarWheelRotator : MonoBehaviour
|
|
{
|
|
[SerializeField] private NavMeshAgent agent;
|
|
[SerializeField] private Transform[] wheels;
|
|
[SerializeField] private bool rotateDebug;
|
|
|
|
private void Update()
|
|
{
|
|
Vector3 wheelRotation;
|
|
foreach (var wheel in wheels)
|
|
{
|
|
if (rotateDebug)
|
|
{
|
|
UnityEngine.Debug.DrawRay(
|
|
wheel.position,
|
|
Vector3.Project(agent.velocity, agent.gameObject.transform.right),
|
|
Color.yellow, 900f);
|
|
UnityEngine.Debug.Log(Vector3.Project(agent.velocity, agent.gameObject.transform.right).magnitude);
|
|
}
|
|
|
|
wheelRotation = wheel.localRotation.eulerAngles;
|
|
wheelRotation.y = Vector3.Project(agent.velocity, agent.gameObject.transform.right).magnitude * 10f;
|
|
wheel.localRotation = Quaternion.Euler(wheelRotation);
|
|
}
|
|
}
|
|
}
|
|
} |