Машина, камера, мотор

This commit is contained in:
ThomasCharester committed 2026-02-06 15:08:14 +03:00
1 parent b54083521f
commit a45223d0e7
48 files changed
+5507 -3735

No files matched your search

@@ -5,6 +5,7 @@ namespace Code.Example.Race.Racer.AI
{
public class PlayerRacer : Racer
{
public Transform CameraPosition;
public override void SetData(RacerData data)
{
Data = data;
@@ -1,6 +1,7 @@
using UnityEngine;
using System;
using System.Collections.Generic;
using Code.Example.Race.Racer.AI;
namespace Code.Example.Race.Racer.Player
{
@@ -30,20 +31,24 @@ namespace Code.Example.Race.Racer.Player
public ControlMode control;
public float maxAcceleration = 30.0f;
public float brakeAcceleration = 50.0f;
public float turnSensitivity = 1.0f;
public float maxSteerAngle = 30.0f;
// public float maxAcceleration = 30.0f;
// public float brakeAcceleration = 50.0f;
//
// public float turnSensitivity = 1.0f;
// public float maxSteerAngle = 30.0f;
public Vector3 _centerOfMass;
public List<Wheel> wheels;
float moveInput;
float steerInput;
// float moveInput;
// float steerInput;
private Rigidbody carRb;
[SerializeField] private PlayerCarInput _playerCarInput;
[SerializeField] private PlayerRacer _playerRacer;
// private CarLights carLights;
@@ -52,47 +57,47 @@ namespace Code.Example.Race.Racer.Player
carRb = GetComponent<Rigidbody>();
carRb.centerOfMass = _centerOfMass;
carLights = GetComponent<CarLights>();
// carLights = GetComponent<CarLights>();
}
void Update()
{
GetInputs();
// GetInputs();
AnimateWheels();
WheelEffects();
// WheelEffects();
}
void LateUpdate()
void FixedUpdate()
{
Move();
Steer();
Brake();
}
public void MoveInput(float input)
{
moveInput = input;
}
public void SteerInput(float input)
{
steerInput = input;
}
// public void MoveInput(float input)
// {
// moveInput = input;
// }
//
// public void SteerInput(float input)
// {
// steerInput = input;
// }
void GetInputs()
{
if (control == ControlMode.Keyboard)
{
moveInput = Input.GetAxis("Vertical");
steerInput = Input.GetAxis("Horizontal");
}
// if (control == ControlMode.Keyboard)
// {
// moveInput = Input.GetAxis("Vertical");
// steerInput = Input.GetAxis("Horizontal");
// }
}
void Move()
{
foreach (var wheel in wheels)
{
wheel.wheelCollider.motorTorque = moveInput * 600 * maxAcceleration * Time.deltaTime;
wheel.wheelCollider.motorTorque = _playerCarInput.MovementInput.y * 600 * _playerRacer.Data.maxAcceleration * Time.fixedDeltaTime;
}
}
@@ -100,25 +105,25 @@ namespace Code.Example.Race.Racer.Player
{
foreach (var wheel in wheels)
{
if (wheel.axel == Axel.Front)
{
var _steerAngle = steerInput * turnSensitivity * maxSteerAngle;
wheel.wheelCollider.steerAngle = Mathf.Lerp(wheel.wheelCollider.steerAngle, _steerAngle, 0.6f);
}
if (wheel.axel != Axel.Front) continue;
var _steerAngle = _playerCarInput.MovementInput.x * _playerRacer.Data.turnSensitivity * _playerRacer.Data.maxSteerAngle;
wheel.wheelCollider.steerAngle = _steerAngle;
// wheel.wheelCollider.steerAngle = Mathf.Lerp(wheel.wheelCollider.steerAngle, _steerAngle, 0.6f);
}
}
void Brake()
{
if (Input.GetKey(KeyCode.Space) || moveInput == 0)
if (_playerCarInput.Brake || _playerCarInput.MovementInput.y == 0)
{
foreach (var wheel in wheels)
{
wheel.wheelCollider.brakeTorque = 300 * brakeAcceleration * Time.deltaTime;
wheel.wheelCollider.brakeTorque = 300 * _playerRacer.Data.brakeAcceleration * Time.fixedDeltaTime;
}
carLights.isBackLightOn = true;
carLights.OperateBackLights();
// carLights.isBackLightOn = true;
// carLights.OperateBackLights();
}
else
{
@@ -127,8 +132,8 @@ namespace Code.Example.Race.Racer.Player
wheel.wheelCollider.brakeTorque = 0;
}
carLights.isBackLightOn = false;
carLights.OperateBackLights();
// carLights.isBackLightOn = false;
// carLights.OperateBackLights();
}
}
@@ -150,15 +155,15 @@ namespace Code.Example.Race.Racer.Player
{
//var dirtParticleMainSettings = wheel.smokeParticle.main;
if (Input.GetKey(KeyCode.Space) && wheel.axel == Axel.Rear && wheel.wheelCollider.isGrounded == true &&
if (wheel.axel == Axel.Rear && wheel.wheelCollider.isGrounded &&
carRb.linearVelocity.magnitude >= 10.0f)
{
wheel.wheelEffectObj.GetComponentInChildren<TrailRenderer>().emitting = true;
wheel.smokeParticle.Emit(1);
// wheel.wheelEffectObj.GetComponentInChildren<TrailRenderer>().emitting = true;
// wheel.smokeParticle.Emit(1);
}
else
{
wheel.wheelEffectObj.GetComponentInChildren<TrailRenderer>().emitting = false;
// wheel.wheelEffectObj.GetComponentInChildren<TrailRenderer>().emitting = false;
}
}
}
@@ -9,6 +9,7 @@ namespace Code.Example.Race.Racer.Player
{
private IInputService _inputService;
public Vector2 MovementInput { get; private set; }
public bool Brake { get; private set; }
[Inject]
public void Construct(IInputService inputService)
@@ -16,17 +17,25 @@ namespace Code.Example.Race.Racer.Player
_inputService = inputService;
_inputService.BindActionPerformed((int)ActionMappings.Gameplay, (int)GameplayActions.Move, MovementPerformed);
_inputService.BindActionCancel((int)ActionMappings.Gameplay, (int)GameplayActions.Move, MovementEnded);
_inputService.BindActionPerformed((int)ActionMappings.Gameplay, (int)GameplayActions.Brake, BrakePerformed);
_inputService.BindActionCancel((int)ActionMappings.Gameplay, (int)GameplayActions.Brake, BrakeEnded);
}
private void MovementPerformed(InputAction.CallbackContext context) => MovementInput = context.ReadValue<Vector2>();
private void MovementEnded(InputAction.CallbackContext context) => MovementInput = Vector2.zero;
private void BrakePerformed(InputAction.CallbackContext context) => Brake = context.ReadValue<bool>();
private void BrakeEnded(InputAction.CallbackContext context) => Brake = false;
private void OnDestroy()
{
if (_inputService == null) return;
_inputService.UnbindActionPerformed((int)ActionMappings.Gameplay, (int)GameplayActions.Move, MovementPerformed);
_inputService.UnbindActionCanceled((int)ActionMappings.Gameplay, (int)GameplayActions.Move, MovementEnded);
_inputService.UnbindActionPerformed((int)ActionMappings.Gameplay, (int)GameplayActions.Brake, BrakePerformed);
_inputService.UnbindActionCanceled((int)ActionMappings.Gameplay, (int)GameplayActions.Brake, BrakeEnded);
}
}
}