Как повернуть виртуальную камеру с помощью клавиатуры и новой системы ввода Unity 6?C#

Место общения программистов C#
Ответить Пред. темаСлед. тема
Anonymous
 Как повернуть виртуальную камеру с помощью клавиатуры и новой системы ввода Unity 6?

Сообщение Anonymous »

Я новичок в программе, и у меня есть проблема, касающаяся управления камерой в моей 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
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «C#»