init
This commit is contained in:
@@ -0,0 +1,102 @@
|
||||
using System;
|
||||
using RPGCore.Movement.ObjectModules.UnitMovement;
|
||||
using RPGCore.Movement.ObjectModules.UnitMovement.Events;
|
||||
using RPGCore.Core;
|
||||
using RPGCore.Core.Objects;
|
||||
using RPGCore.ObjectModules.EventObjectModule;
|
||||
using RPGCoreCommon.Helpers;
|
||||
using UnityEngine;
|
||||
|
||||
namespace RPGCore.Movement.ObjectModules.UnitAnimator
|
||||
{
|
||||
[Serializable]
|
||||
[RequireComponent(typeof(UnitObject))]
|
||||
[RequireComponent(typeof(UnitMovementModule))]
|
||||
public class UnitAnimatorModule : ObjectModule<UnitObject>
|
||||
{
|
||||
// CONSTANT (defined in animator)
|
||||
private static readonly int BaseLayer = 0;
|
||||
private static readonly int VelocityMagnitude = Animator.StringToHash("VELOCITY_MAGNITUDE");
|
||||
private static readonly int DirectionX = Animator.StringToHash("DIRECTION_X");
|
||||
private static readonly int DirectionZ = Animator.StringToHash("DIRECTION_Z");
|
||||
private static readonly int MoveAnimSpeed = Animator.StringToHash("MOVE_ANIM_SPEED");
|
||||
private static readonly int JumpTrigger = Animator.StringToHash("JUMP_TRIGGER");
|
||||
private static readonly int IsOnGround = Animator.StringToHash("IS_ON_GROUND");
|
||||
|
||||
// SERIALIZED
|
||||
[SerializeField] private Animator _animator;
|
||||
|
||||
[Header("Movement animation speed - used for animation speed scaling")]
|
||||
[SerializeField] private float _sprintSpeed = 6f;
|
||||
[SerializeField] private float _runSpeed = 3f;
|
||||
|
||||
[Header("Animation spine helper")]
|
||||
[SerializeField] private bool _spineHelper;
|
||||
[SerializeField] private Transform _hips;
|
||||
[SerializeField] private Transform _spine;
|
||||
[SerializeField] private Quaternion _spineRotation;
|
||||
|
||||
private void OnValidate()
|
||||
{
|
||||
if (!_animator)
|
||||
{
|
||||
_animator = parent.GetComponentInChildren<Animator>();
|
||||
UnityEditor.EditorUtility.SetDirty(parent.gameObject);
|
||||
}
|
||||
|
||||
if (_animator)
|
||||
{
|
||||
_hips = _animator.GetBoneTransform(HumanBodyBones.Hips);
|
||||
_spine = _animator.GetBoneTransform(HumanBodyBones.Spine);
|
||||
_spineRotation = _spine.localRotation;
|
||||
}
|
||||
}
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
parent.events.Register<JumpEvent>(_ => _animator.SetTrigger(JumpTrigger));
|
||||
parent.events.Register<LandEvent>(_ => _animator.SetBool(IsOnGround, true));
|
||||
parent.events.Register<FallEvent>(_ => _animator.SetBool(IsOnGround, false));
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
_animator.SetBool(IsOnGround, parent.GetComponent<UnitMovementModule>().isOnGround);
|
||||
}
|
||||
|
||||
private void LateUpdate()
|
||||
{
|
||||
if (_spineHelper) _spine.rotation = _hips.rotation * _spineRotation;
|
||||
}
|
||||
|
||||
private void FixedUpdate()
|
||||
{
|
||||
HandleMoveAnimation();
|
||||
}
|
||||
|
||||
private void HandleMoveAnimation()
|
||||
{
|
||||
var unitMovement = parent.GetComponent<UnitMovementModule>();
|
||||
if (!unitMovement.isMoving)
|
||||
{
|
||||
_animator.SetFloat(VelocityMagnitude, 0f);
|
||||
_animator.SetFloat(DirectionX, 0f);
|
||||
_animator.SetFloat(DirectionZ, 0f);
|
||||
_animator.SetFloat(MoveAnimSpeed, 1f);
|
||||
return;
|
||||
}
|
||||
|
||||
// TODO: tutaj powinno jakoś fajnie brać prędkość TYLKO od ruchu, a nie całego velocity
|
||||
var relVelocity = parent.transform.InverseTransformDirection(parent.rigidbody.linearVelocity.SetY(0));
|
||||
var moveAnimSpeed = relVelocity.magnitude < _runSpeed ? relVelocity.magnitude / _runSpeed
|
||||
: relVelocity.magnitude > _sprintSpeed ? relVelocity.magnitude / _sprintSpeed
|
||||
: 1f;
|
||||
var moveRelativeDirection = relVelocity.normalized;
|
||||
|
||||
_animator.SetFloat(VelocityMagnitude, relVelocity.magnitude);
|
||||
_animator.SetFloat(DirectionX, moveRelativeDirection.x);
|
||||
_animator.SetFloat(DirectionZ, moveRelativeDirection.z);
|
||||
_animator.SetFloat(MoveAnimSpeed, moveAnimSpeed);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2196f6792fb64519a9c02abec4cad026
|
||||
timeCreated: 1767463517
|
||||
Reference in New Issue
Block a user