turning slowly, waiting for standing animation when standing

This commit is contained in:
Vladimir Koshevarov
2023-02-28 13:21:43 +02:00
parent 81b266c288
commit c62b8ddd32
2 changed files with 50 additions and 22 deletions
@@ -5,8 +5,6 @@ public class Bed : BaseInteractableObject
{
public override void Interact(Player player)
{
Debug.Log("rotate player");
player.Rotate(_playerArrivePoint.transform.forward);
Debug.Log("Sitting");
player.SetPlayerState(ActionStates.Sitting);
}
+50 -20
View File
@@ -3,9 +3,10 @@ using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public enum Tasks { Move, Interact };
public enum Tasks { Move, Interact, Rotate };
public enum TaskStatus { Waiting, InProgress, Complete };
public enum ActionStates { Idle, Walking, Sleeping, Sitting, Standing };
public class Player : MonoBehaviour
{
public static Player Instance { get; private set; }
@@ -30,9 +31,9 @@ public class Player : MonoBehaviour
private const string WALK = "Walk";
private const string WALK_VELOCITY = "WalkVelocity";
private bool _isInAnimation = false;
private void Awake()
{
if (Instance != null)
{
Debug.Log("There's more than one player instance");
@@ -55,6 +56,16 @@ public class Player : MonoBehaviour
private void Update()
{
if (_isInAnimation)
{
if (!isAnimationStatePlaying(0, "SitToStand"))
{
_isInAnimation = false;
}
else
return;
}
if (_currentTask == null || _currentTask.Status == TaskStatus.Complete)
{
_tasks.TryDequeue(out _currentTask);
@@ -65,7 +76,15 @@ public class Player : MonoBehaviour
Debug.Log($"Current task {_currentTask.Task}");
switch (_currentTask.Task)
{
case Tasks.Rotate:
_currentTask.UpdateStatus(Rotate(_currentTask.TagretObject._playerArrivePoint.forward));
break;
case Tasks.Move:
if (_currentState == ActionStates.Sitting)
{
SetPlayerState(ActionStates.Standing);
return;
}
_navAgent.SetDestination(_currentTask.TagretObject._playerArrivePoint.position);
_currentTask.UpdateStatus(MoveToPoint());
break;
@@ -75,6 +94,7 @@ public class Player : MonoBehaviour
else
{
AddTask(new PlayerTasks(Tasks.Move, _currentTask.TagretObject));
AddTask(new PlayerTasks(Tasks.Rotate, _currentTask.TagretObject));
AddTask(_currentTask);
_currentTask = null;
}
@@ -86,37 +106,43 @@ public class Player : MonoBehaviour
private TaskStatus MoveToPoint()
{
SetPlayerState(ActionStates.Walking);
var worldDeltaPosition = _navAgent.nextPosition - transform.position;
_groundDeltaPosition.x = Vector3.Dot(transform.right, worldDeltaPosition);
_groundDeltaPosition.y = Vector3.Dot(transform.forward, worldDeltaPosition);
Vector2 velocity = (Time.deltaTime > 1e-5f) ? _groundDeltaPosition / Time.deltaTime : Vector2.zero;
_animator.SetFloat(WALK_VELOCITY, velocity.y);
return pathComplete(_navAgent.destination) ? TaskStatus.Complete : TaskStatus.InProgress;
}
private bool pathComplete(Vector3 destination)
{
if (Vector3.Distance(destination, _navAgent.transform.position) <= _navAgent.radius)
{
if (Vector3.Distance(destination, _navAgent.transform.position) <= _navAgent.radius)
if (!_navAgent.hasPath || _navAgent.velocity.sqrMagnitude == 0f)
{
if (!_navAgent.hasPath || _navAgent.velocity.sqrMagnitude == 0f)
{
SetPlayerState(ActionStates.Idle);
return true;
}
SetPlayerState(ActionStates.Idle);
return true;
}
return false;
}
public void Rotate(Vector3 target)
{
Quaternion rotation = Quaternion.LookRotation(target);
transform.rotation = rotation;
return false;
}
public TaskStatus Rotate(Vector3 target)
{
var targetRot = Quaternion.LookRotation(target);
Quaternion rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(target), 10 * Time.deltaTime);
rotation.x = 0;
transform.rotation = rotation;
if (targetRot == transform.rotation)
{
return TaskStatus.Complete;
}
return TaskStatus.InProgress;
}
private TaskStatus InteractWithObject(BaseInteractableObject interactableObject)
{
@@ -126,8 +152,11 @@ public class Player : MonoBehaviour
public void SetPlayerState(ActionStates newState)
{
if(newState== _currentState)
{ return; }
if (newState == _currentState)
{
return;
}
_isInAnimation = true;
switch (newState)
{
case ActionStates.Idle:
@@ -145,10 +174,11 @@ public class Player : MonoBehaviour
_animator.SetBool(SIT_DOWN, false);
break;
}
_currentState = newState;
}
bool isAnimationStatePlaying(int animLayer, string stateName)
{
if (_animator.GetCurrentAnimatorStateInfo(animLayer).IsName(stateName) &&