BITKit/Packages/Common~/Extensions/AI/EntityAIMovement.cs

67 lines
2.5 KiB
C#
Raw Normal View History

2023-06-05 19:57:17 +08:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
using Mirror;
namespace BITKit.Entities
{
public class EntityAIMovement : EntityComponent
{
[Header(Constant.Header.Components)]
public EntityAi ai;
public NavMeshAgent agent;
public Animator animator;
[Header(Constant.Header.Reference)]
[SerializeReference, SubclassSelector] public References _rootVelocity;
[SerializeReference, SubclassSelector] public References _velocity;
[SerializeReference, SubclassSelector] public References _vertical;
[SerializeReference, SubclassSelector] public References _horizontal;
[SerializeReference, SubclassSelector] public References _isGrounded;
[Header(Constant.Header.InternalVariables)]
Vector3 velocity;
[ServerCallback]
public override void OnFixedUpdate(float deltaTime)
{
switch (ai.currentState)
{
case AIState.Idle:
case AIState.Seek:
case AIState.Movement:
case AIState.Escape:
var direction = agent.nextPosition - transform.position;
velocity = transform.InverseTransformDirection(agent.velocity);
entity.Set<Vector3>(_velocity, velocity);
entity.Set<float>(_vertical, velocity.z);
entity.Set<float>(_horizontal, velocity.x);
entity.Set<bool>(_isGrounded, agent.isOnOffMeshLink is false);
direction = Vector3.ProjectOnPlane(-direction, Vector3.up);
if (direction.sqrMagnitude is not 0)
{
//transform.rotation = Quaternion.LookRotation(direction);
}
agent.updateRotation = true;
break;
case AIState.Attack:
case AIState.HitStop:
agent.updateRotation = false;
agent.velocity = animator.velocity;
break;
}
}
[ServerCallback]
void OnAnimatorMove()
{
entity.Set<Vector3>(_rootVelocity, animator.velocity);
//agent.velocity = animator.velocity;
//transform.position += animator.deltaPosition;
}
void OnDrawGizmos()
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(agent.nextPosition, 0.1f);
}
}
}