BITFALL/Assets/Artists/Scripts/Entity/ThirdPerson/EntityThirdPersonView.cs

68 lines
2.4 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
using BITKit;
using BITKit.Entities;
namespace BITFALL
{
public class EntityThirdPersonView : EntityInputComponent
{
[Header(Constant.Header.Settings)]
[Header(Constant.Header.Gameobjects)]
public Transform cameraRoot;
[Header(Constant.Header.Reference)]
[SerializeReference, SubclassSelector] public References _cameraRotation;
[SerializeReference, SubclassSelector] References _moveRotation;
[SerializeReference, SubclassSelector] public References _ads;
[SerializeReference, SubclassSelector] public References _angularVelocity;
[Header(Constant.Header.InternalVariables)]
Vector2 lookInput;
Vector2 moveInput;
Vector2 rot;
public override void OnAwake()
{
base.OnAwake();
rot = MathV.TransientRotationAxis(transform.eulerAngles);
}
public override void OnStart()
{
base.OnStart();
entity.AddListener<InputAction.CallbackContext>(nameof(OnView), OnView);
entity.AddListener<InputAction.CallbackContext>(nameof(OnMovement), OnMovement);
}
public override void OnView(InputAction.CallbackContext context)
{
var playerConfig = Data.Get<PlayerConfig>();
var ads = Data.Get<float>(_ads);
var raw = context.ReadValue<Vector2>() * playerConfig.sensitivity * playerConfig.m_yaw * ads;
var _lookInput = raw;
lookInput.x -= _lookInput.y;
lookInput.y += _lookInput.x;
lookInput.x = Mathf.Clamp(lookInput.x, -80, 80);
entity.Set(_angularVelocity, raw);
}
public override void OnMovement(InputAction.CallbackContext context)
{
moveInput = context.ReadValue<Vector2>();
}
public override void OnLateUpdate(float deltaTime)
{
var rotation = Quaternion.Euler(lookInput);
Vector3 moveEulur = new(moveInput.x, 0, moveInput.y);
var moveAngle = Mathf.Atan2(moveInput.x, moveInput.y) * Mathf.Rad2Deg +
lookInput.y;
var moveRotation = Quaternion.Euler(0, moveAngle, 0);
cameraRoot.rotation = rotation;
entity.Set(_cameraRotation, rotation);
entity.Set(_moveRotation, moveRotation);
}
}
}