edited car
This commit is contained in:
1 parent
1a6fa8c63c
commit
8e168efa3e
7 files changed
+742
-41
No files matched your search
@@ -4,16 +4,20 @@ using UnityEngine;
|
||||
|
||||
namespace Code.Gameplay
|
||||
{
|
||||
[RequireComponent(typeof(Rigidbody))]
|
||||
public class CarController : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private float motorForce = 150000f;
|
||||
[SerializeField] private float brakeForce = 300000f;
|
||||
[Header("Physics")]
|
||||
[SerializeField] private float motorForce = 150000;
|
||||
[SerializeField] private float brakeForce = 300000;
|
||||
[SerializeField] private float maxSteerAngle = 25f;
|
||||
[SerializeField] private float turnSpeed = 50f;
|
||||
|
||||
[SerializeField] private float turnSpeed = 50;
|
||||
|
||||
[Header("Wheels")]
|
||||
[SerializeField] private Transform frontLeftWheel;
|
||||
[SerializeField] private Transform frontRightWheel;
|
||||
|
||||
|
||||
[Header("Camera")]
|
||||
[SerializeField] private Transform driverCameraPoint;
|
||||
[SerializeField] private Camera mainCamera;
|
||||
[SerializeField] private float thirdPersonDistance = 6f;
|
||||
@@ -23,12 +27,12 @@ namespace Code.Gameplay
|
||||
private Rigidbody _rb;
|
||||
private bool _isFirstPerson;
|
||||
private IInputService _inputService;
|
||||
private float _lastSwitchTime;
|
||||
private const float SwitchCooldown = 0.5f;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
_rb = GetComponent<Rigidbody>();
|
||||
_rb.useGravity = true;
|
||||
_rb.interpolation = RigidbodyInterpolation.Interpolate;
|
||||
}
|
||||
|
||||
private void Start()
|
||||
@@ -42,9 +46,13 @@ namespace Code.Gameplay
|
||||
{
|
||||
if (_inputService.SwitchCameraPressed)
|
||||
{
|
||||
_isFirstPerson = !_isFirstPerson;
|
||||
if (_isFirstPerson) SwitchToFirstPerson();
|
||||
else SwitchToThirdPerson();
|
||||
if (Time.time - _lastSwitchTime >= SwitchCooldown)
|
||||
{
|
||||
_isFirstPerson = !_isFirstPerson;
|
||||
if (_isFirstPerson) SwitchToFirstPerson();
|
||||
else SwitchToThirdPerson();
|
||||
_lastSwitchTime = Time.time;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -52,51 +60,56 @@ namespace Code.Gameplay
|
||||
{
|
||||
var input = _inputService.MoveInput;
|
||||
|
||||
var force = transform.forward * input.y * motorForce;
|
||||
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);
|
||||
_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);
|
||||
var steerInput = input.x;
|
||||
if (input.y < 0)
|
||||
steerInput = -input.x;
|
||||
|
||||
var steer = steerInput * maxSteerAngle;
|
||||
|
||||
if (frontLeftWheel) frontLeftWheel.localRotation = Quaternion.Euler(0, steer, 0);
|
||||
if (frontRightWheel) frontRightWheel.localRotation = Quaternion.Euler(0, steer, 0);
|
||||
|
||||
if (!(_rb.linearVelocity.magnitude > 0.5f)) return;
|
||||
|
||||
var turn = input.x * turnSpeed * Time.fixedDeltaTime;
|
||||
var turn = steerInput * turnSpeed * Time.fixedDeltaTime;
|
||||
_rb.MoveRotation(_rb.rotation * Quaternion.Euler(0, turn, 0));
|
||||
}
|
||||
|
||||
private void LateUpdate()
|
||||
{
|
||||
if (mainCamera == null) return;
|
||||
if (!mainCamera) return;
|
||||
|
||||
if (_isFirstPerson)
|
||||
{
|
||||
if (driverCameraPoint == null) return;
|
||||
|
||||
mainCamera.transform.position = driverCameraPoint.position;
|
||||
mainCamera.transform.rotation = driverCameraPoint.rotation;
|
||||
if (driverCameraPoint)
|
||||
{
|
||||
mainCamera.transform.localPosition = driverCameraPoint.localPosition;
|
||||
mainCamera.transform.localRotation = driverCameraPoint.localRotation;
|
||||
}
|
||||
}
|
||||
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);
|
||||
mainCamera.transform.localPosition = new Vector3(0, 5, -8);
|
||||
mainCamera.transform.localRotation = Quaternion.Euler(20, 0, 0);
|
||||
}
|
||||
}
|
||||
|
||||
private void SwitchToFirstPerson()
|
||||
{
|
||||
if (mainCamera != null) mainCamera.fieldOfView = 75;
|
||||
if (mainCamera) mainCamera.fieldOfView = 75;
|
||||
}
|
||||
|
||||
private void SwitchToThirdPerson()
|
||||
{
|
||||
if (mainCamera != null) mainCamera.fieldOfView = 60;
|
||||
if (mainCamera) mainCamera.fieldOfView = 60;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user