Files
Infratest/Assets/Code/Example/Race/Racer/Player/PlayerCarController.cs
T

55 lines
2.4 KiB
C#

using Code.Example.Race.Racer.AI;
using Code.Example.Race.Racer.Car;
using UnityEngine;
namespace Code.Example.Race.Racer.Player
{
public class PlayerCarController : MonoBehaviour
{
[SerializeField] private Rigidbody _rigidbody;
[SerializeField] private PlayerCarInput _playerCarInput;
[SerializeField] private PlayerRacer _playerRacer;
[SerializeField] private CarWheelRotatorPlayer _wheels;
private void Update()
{
if (_playerCarInput.MovementInput == Vector2.zero) return;
// var movementForward = transform.forward * (_playerCarInput.MovementInput.y * _playerRacer.Data.speed);
//
// _rigidbody.AddForce(movementForward * Time.fixedDeltaTime, ForceMode.Force);
var force = transform.forward * (_playerCarInput.MovementInput.y * _playerRacer.Data.speed);
_rigidbody.AddForce(force, ForceMode.Force);
// if (Mathf.Abs(_playerCarInput.MovementInput.y) < 0.1f && _rigidbody.linearVelocity.magnitude > 0.3f)
// {
// _rigidbody.AddForce(-_rigidbody.linearVelocity * (_playerRacer.Data.brakeForce * Time.fixedDeltaTime),
// ForceMode.Force);
// }
var steerInput = _playerCarInput.MovementInput.x;
if (_playerCarInput.MovementInput.y < 0)
steerInput = -_playerCarInput.MovementInput.x;
// var steer = steerInput * maxSteerAngle;
//
// if (frontLeftWheel) frontLeftWheel.localRotation = Quaternion.Euler(0, steer, 0);
// if (frontRightWheel) frontRightWheel.localRotation = Quaternion.Euler(0, steer, 0);
if (!(_rigidbody.linearVelocity.magnitude > 0.5f)) return;
// var turn = steerInput * _playerRacer.Data.turnSpeed * Time.fixedDeltaTime;
// _rigidbody.MoveRotation(_rigidbody.rotation * Quaternion.Euler(0, turn, 0));
if (!(_rigidbody.linearVelocity.magnitude is > 1f or < -1f)) return;
var rotateAmount = _playerCarInput.MovementInput.x * _playerRacer.Data.angularSpeed;
_wheels.RotateWheels(rotateAmount * 10 * Time.fixedDeltaTime);
var wheelRotation = transform.rotation.eulerAngles;
wheelRotation.y += rotateAmount;
transform.rotation = Quaternion.Euler(wheelRotation);
}
}
}