Я новичок в программе, и у меня есть проблема, касающаяся управления камерой в моей 3D -стратегической игре. Мне удалось достичь желаемого результата, используя старую систему ввода, но я не могу понять это в новой системе ввода.using System;
using Unity.Mathematics;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.Rendering;
public class CameraController : MonoBehaviour
{
private PlayerControls cameraActions;
private InputAction movement;
private Transform cameraTransform;
private InputAction rotation;
[SerializeField]
private float maxSpeed = 5f;
private float currentSpeed;
[SerializeField]
private float acceleration = 10f;
[SerializeField]
private float damping = 15f;
[SerializeField]
private float maxRotationSpeed = 1f;
private Vector3 targetPosition;
private Vector3 horizontalVelocity;
private Vector3 lastPosition;
Vector3 startDrag;
private void Awake()
{
cameraActions = new PlayerControls();
cameraTransform = this.GetComponentInChildren().transform;
}
private void OnEnable()
{
lastPosition = this.transform.position;
movement = cameraActions.Camera.Movement;
cameraActions.Camera.Enable();
cameraActions.Camera.CamRotation.performed += RotateCamera;
}
private void OnDisable()
{
cameraActions.Camera.CamRotation.performed -= RotateCamera;
cameraActions.Camera.Disable();
}
private void Update()
{
GetKeyboardMovement();
UpdateVelocity();
UpdateBasedPosition();
Vector2 rotationValue = rotation.ReadValue();
}
private void UpdateVelocity()
{
horizontalVelocity = (this.transform.position - lastPosition) / Time.deltaTime;
horizontalVelocity.y = 0;
lastPosition = this.transform.position;
}
private void GetKeyboardMovement()
{
Vector3 inputValue = movement.ReadValue().x * GetCameraRight()
+ movement.ReadValue().y * GetCameraForward();
inputValue = inputValue.normalized;
if (inputValue.sqrMagnitude > 0.1f)
targetPosition += inputValue;
}
private Vector3 GetCameraRight()
{
Vector3 right = cameraTransform.right;
right.y = 0;
return right;
}
private Vector3 GetCameraForward()
{
Vector3 forward = cameraTransform.forward;
forward.y = 0;
return forward;
}
private void UpdateBasedPosition()
{
if (targetPosition.sqrMagnitude > 0.1f)
{
currentSpeed = Mathf.Lerp(currentSpeed, maxSpeed, Time.deltaTime * acceleration);
transform.position += targetPosition * currentSpeed * Time.deltaTime;
}
else
{
horizontalVelocity = Vector3.Lerp(horizontalVelocity, Vector3.zero, Time.deltaTime * damping);
transform.position += horizontalVelocity * Time.deltaTime;
}
targetPosition = Vector3.zero;
}
private void RotateCamera(InputAction.CallbackContext inputValue)
{
if (!Mouse.current.middleButton.isPressed)
return;
float value = inputValue.ReadValue().x;
transform.rotation = Quaternion.Euler(0f, y: value * maxRotationSpeed * Time.deltaTime, 0f);
}
}
< /code>
Мои входные действия < /p>
Желаемый результат, работая со старой системой ввода < /p>
Я попытался добавить новое привязку вектора 2 с связанными клавишами q и e, и оттуда создать метод для вращения камеры. Это не сработало, и ни один из них не показал никаких ошибок, на самом деле это казалось, что ничего не произошло. И ничего снова.
Подробнее здесь: https://stackoverflow.com/questions/796 ... put-system
Как повернуть виртуальную камеру с помощью клавиатуры и новой системы ввода Unity 6? ⇐ C#
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
Как повернуть виртуальную камеру с помощью клавиатуры и новой системы ввода Unity 6?
Anonymous » » в форуме C# - 0 Ответы
- 11 Просмотры
-
Последнее сообщение Anonymous
-
-
-
Как повернуть виртуальную камеру с помощью клавиатуры и новой системы ввода Unity 6?
Anonymous » » в форуме C# - 0 Ответы
- 6 Просмотры
-
Последнее сообщение Anonymous
-