ошибки поворота, прицела
This commit is contained in:
1 parent
202a894140
commit
da648df1d9
200 files changed
+31969
-629
No files matched your search
@@ -10,8 +10,11 @@ public class PlayerMovement : MonoBehaviour
|
||||
public float sneakSpeed = 1.5f; // Тактический шаг (Ctrl)
|
||||
|
||||
[Header("Настройки вращения")]
|
||||
public float turnSmoothTime = 0.1f; // Плавность поворота модельки
|
||||
public float turnSmoothTime = 0.1f; // Плавность поворота модельки при свободном беге
|
||||
[Tooltip("Плавность поворота к прицелу при стрельбе/прицеливании (быстро, но без телепорта)")]
|
||||
public float aimTurnSmoothTime = 0.05f;
|
||||
private float turnSmoothVelocity;
|
||||
private float aimTurnSmoothVelocity;
|
||||
|
||||
[Header("Камера")]
|
||||
public Transform cam; // Ссылка на главную камеру
|
||||
@@ -23,10 +26,16 @@ public class PlayerMovement : MonoBehaviour
|
||||
private Vector3 velocity;
|
||||
private float gravity = -9.81f;
|
||||
private float currentAnimSpeed = 0f;
|
||||
private float aimLockTimer = 0f;
|
||||
|
||||
public bool IsAimLocked => aimLockTimer > 0f;
|
||||
|
||||
void Start()
|
||||
{
|
||||
controller = GetComponent<CharacterController>();
|
||||
if (cam == null && Camera.main != null)
|
||||
cam = Camera.main.transform;
|
||||
|
||||
if (animator == null)
|
||||
animator = GetComponentInChildren<Animator>();
|
||||
|
||||
@@ -41,6 +50,23 @@ public class PlayerMovement : MonoBehaviour
|
||||
Cursor.visible = false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Активирует быстрый плавный разворот персонажа к прицелу камеры (для стрельбы/ударов)
|
||||
/// и удерживает режим стрейфа на заданное время.
|
||||
/// </summary>
|
||||
public void SnapToAim(float lockDuration = 0.5f)
|
||||
{
|
||||
if (cam == null && Camera.main != null)
|
||||
cam = Camera.main.transform;
|
||||
|
||||
if (aimLockTimer <= 0f)
|
||||
{
|
||||
aimTurnSmoothVelocity = 0f;
|
||||
}
|
||||
|
||||
aimLockTimer = Mathf.Max(aimLockTimer, lockDuration);
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
HandleMovement();
|
||||
@@ -48,6 +74,18 @@ public class PlayerMovement : MonoBehaviour
|
||||
|
||||
void HandleMovement()
|
||||
{
|
||||
if (cam == null && Camera.main != null)
|
||||
cam = Camera.main.transform;
|
||||
|
||||
if (aimLockTimer > 0f)
|
||||
{
|
||||
aimLockTimer -= Time.deltaTime;
|
||||
if (aimLockTimer <= 0f)
|
||||
{
|
||||
turnSmoothVelocity = 0f;
|
||||
}
|
||||
}
|
||||
|
||||
// Гравитация
|
||||
if (controller.isGrounded && velocity.y < 0)
|
||||
{
|
||||
@@ -76,12 +114,37 @@ public class PlayerMovement : MonoBehaviour
|
||||
Vector3 direction = new Vector3(horizontal, 0f, vertical).normalized;
|
||||
bool isMoving = direction.magnitude >= 0.1f;
|
||||
|
||||
if (isMoving)
|
||||
if (aimLockTimer > 0f)
|
||||
{
|
||||
// Вычисляем угол поворота с учетом направления камеры
|
||||
// При стрельбе/прицеливании персонаж быстро и плавно поворачивается в сторону прицела камеры
|
||||
if (cam != null)
|
||||
{
|
||||
float targetAngle = cam.eulerAngles.y;
|
||||
float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref aimTurnSmoothVelocity, aimTurnSmoothTime);
|
||||
transform.rotation = Quaternion.Euler(0f, angle, 0f);
|
||||
}
|
||||
|
||||
// Перемещение работает в режиме стрейфа относительно камеры (нажатие назад = отступать спиной к камере)
|
||||
if (isMoving && cam != null)
|
||||
{
|
||||
Vector3 camFwd = cam.forward;
|
||||
camFwd.y = 0f;
|
||||
camFwd.Normalize();
|
||||
|
||||
Vector3 camRight = cam.right;
|
||||
camRight.y = 0f;
|
||||
camRight.Normalize();
|
||||
|
||||
Vector3 moveDir = (camFwd * vertical + camRight * horizontal).normalized;
|
||||
controller.Move(moveDir * currentSpeed * Time.deltaTime);
|
||||
}
|
||||
}
|
||||
else if (isMoving)
|
||||
{
|
||||
// Свободный бег: вычисляем угол поворота с учетом направления камеры
|
||||
float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
|
||||
|
||||
// Плавно поворачиваем капсулу
|
||||
// Плавно поворачиваем капсулу в сторону бега
|
||||
float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
|
||||
transform.rotation = Quaternion.Euler(0f, angle, 0f);
|
||||
|
||||
|
||||
Reference in new issue
Block a user