basic car implementation

This commit is contained in:
MrTorex committed 2026-01-27 14:50:17 +03:00
1 parent 01ce057601
commit 1a6fa8c63c
49 files changed
+3654 -9

No files matched your search

+3
View File
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: a0c9daaed4d8456fbe340b0bd7e144b5
timeCreated: 1769511327
+102
View File
@@ -0,0 +1,102 @@
using Code.DI;
using Code.Input;
using UnityEngine;
namespace Code.Gameplay
{
public class CarController : MonoBehaviour
{
[SerializeField] private float motorForce = 150000f;
[SerializeField] private float brakeForce = 300000f;
[SerializeField] private float maxSteerAngle = 25f;
[SerializeField] private float turnSpeed = 50f;
[SerializeField] private Transform frontLeftWheel;
[SerializeField] private Transform frontRightWheel;
[SerializeField] private Transform driverCameraPoint;
[SerializeField] private Camera mainCamera;
[SerializeField] private float thirdPersonDistance = 6f;
[SerializeField] private float thirdPersonHeight = 2.5f;
[SerializeField] private float cameraFollowSpeed = 4f;
private Rigidbody _rb;
private bool _isFirstPerson;
private IInputService _inputService;
private void Awake()
{
_rb = GetComponent<Rigidbody>();
_rb.useGravity = true;
_rb.interpolation = RigidbodyInterpolation.Interpolate;
}
private void Start()
{
var container = FindFirstObjectByType<DIContainer>();
_inputService = container.Resolve<IInputService>();
SwitchToThirdPerson();
}
private void Update()
{
if (_inputService.SwitchCameraPressed)
{
_isFirstPerson = !_isFirstPerson;
if (_isFirstPerson) SwitchToFirstPerson();
else SwitchToThirdPerson();
}
}
private void FixedUpdate()
{
var input = _inputService.MoveInput;
var force = transform.forward * input.y * motorForce;
_rb.AddForce(force, ForceMode.Force);
if (Mathf.Abs(input.y) < 0.1f && _rb.linearVelocity.magnitude > 0.3f)
{
_rb.AddForce(-_rb.linearVelocity * brakeForce * Time.fixedDeltaTime, ForceMode.Force);
}
var steer = input.x * maxSteerAngle;
if (frontLeftWheel != null) frontLeftWheel.localRotation = Quaternion.Euler(0, steer, 0);
if (frontRightWheel != null) frontRightWheel.localRotation = Quaternion.Euler(0, steer, 0);
if (!(_rb.linearVelocity.magnitude > 0.5f)) return;
var turn = input.x * turnSpeed * Time.fixedDeltaTime;
_rb.MoveRotation(_rb.rotation * Quaternion.Euler(0, turn, 0));
}
private void LateUpdate()
{
if (mainCamera == null) return;
if (_isFirstPerson)
{
if (driverCameraPoint == null) return;
mainCamera.transform.position = driverCameraPoint.position;
mainCamera.transform.rotation = driverCameraPoint.rotation;
}
else
{
var targetPos = transform.position - transform.forward * thirdPersonDistance + Vector3.up * thirdPersonHeight;
mainCamera.transform.position = Vector3.Lerp(mainCamera.transform.position, targetPos, cameraFollowSpeed * Time.deltaTime);
mainCamera.transform.LookAt(transform.position + Vector3.up * 1f);
}
}
private void SwitchToFirstPerson()
{
if (mainCamera != null) mainCamera.fieldOfView = 75;
}
private void SwitchToThirdPerson()
{
if (mainCamera != null) mainCamera.fieldOfView = 60;
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 7a8286065af8470ab32469e545d7c619
timeCreated: 1769511335
+2 -2
View File
@@ -3,7 +3,7 @@ namespace Code.Input
public enum ActionMapping
{
MainMenu,
UI,
Gameplay
Gameplay,
UI
}
}
+1 -1
View File
@@ -13,7 +13,7 @@ namespace Code.Input
Vector2 LookInput { get; }
Vector2 MoveInput { get; }
bool SprintPressed { get; }
bool SwitchCameraPressed { get; }
void DebugBindAction(int mapId, int actionId, Action<InputAction.CallbackContext> action);
+6 -6
View File
@@ -12,7 +12,7 @@ namespace Code.Input
public Vector2 LookInput { get; private set; }
public Vector2 MoveInput { get; private set; }
public bool SprintPressed { get; private set; }
public bool SwitchCameraPressed { get; private set; }
public InputService(InputActionAsset inputActionAsset)
{
@@ -23,16 +23,16 @@ namespace Code.Input
private void SetupInputCallbacks()
{
SetupMainMenuInputCallbacks();
//SetupMovementInputCallbacks();
SetupGameplayInputCallbacks();
}
/*private void SetupMovementInputCallbacks()
private void SetupGameplayInputCallbacks()
{
var map = _inputActionAsset.actionMaps[(int)ActionMapping.Movement];
var map = _inputActionAsset.actionMaps[(int)ActionMapping.Gameplay];
BindVector2(map, "Look", v => LookInput = v);
BindVector2(map, "Move", v => MoveInput = v);
BindButton(map, "Sprint", v => SprintPressed = v);
}*/
BindButton(map, "SwitchCamera", v => SwitchCameraPressed = v);
}
private void SetupMainMenuInputCallbacks()
{