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) public override void Interact(Player player)
{ {
Debug.Log("rotate player");
player.Rotate(_playerArrivePoint.transform.forward);
Debug.Log("Sitting"); Debug.Log("Sitting");
player.SetPlayerState(ActionStates.Sitting); player.SetPlayerState(ActionStates.Sitting);
} }
+46 -16
View File
@@ -3,9 +3,10 @@ using System.Collections.Generic;
using UnityEngine; using UnityEngine;
using UnityEngine.AI; using UnityEngine.AI;
public enum Tasks { Move, Interact }; public enum Tasks { Move, Interact, Rotate };
public enum TaskStatus { Waiting, InProgress, Complete }; public enum TaskStatus { Waiting, InProgress, Complete };
public enum ActionStates { Idle, Walking, Sleeping, Sitting, Standing }; public enum ActionStates { Idle, Walking, Sleeping, Sitting, Standing };
public class Player : MonoBehaviour public class Player : MonoBehaviour
{ {
public static Player Instance { get; private set; } public static Player Instance { get; private set; }
@@ -30,9 +31,9 @@ public class Player : MonoBehaviour
private const string WALK = "Walk"; private const string WALK = "Walk";
private const string WALK_VELOCITY = "WalkVelocity"; private const string WALK_VELOCITY = "WalkVelocity";
private bool _isInAnimation = false;
private void Awake() private void Awake()
{ {
if (Instance != null) if (Instance != null)
{ {
Debug.Log("There's more than one player instance"); Debug.Log("There's more than one player instance");
@@ -55,6 +56,16 @@ public class Player : MonoBehaviour
private void Update() private void Update()
{ {
if (_isInAnimation)
{
if (!isAnimationStatePlaying(0, "SitToStand"))
{
_isInAnimation = false;
}
else
return;
}
if (_currentTask == null || _currentTask.Status == TaskStatus.Complete) if (_currentTask == null || _currentTask.Status == TaskStatus.Complete)
{ {
_tasks.TryDequeue(out _currentTask); _tasks.TryDequeue(out _currentTask);
@@ -65,7 +76,15 @@ public class Player : MonoBehaviour
Debug.Log($"Current task {_currentTask.Task}"); Debug.Log($"Current task {_currentTask.Task}");
switch (_currentTask.Task) switch (_currentTask.Task)
{ {
case Tasks.Rotate:
_currentTask.UpdateStatus(Rotate(_currentTask.TagretObject._playerArrivePoint.forward));
break;
case Tasks.Move: case Tasks.Move:
if (_currentState == ActionStates.Sitting)
{
SetPlayerState(ActionStates.Standing);
return;
}
_navAgent.SetDestination(_currentTask.TagretObject._playerArrivePoint.position); _navAgent.SetDestination(_currentTask.TagretObject._playerArrivePoint.position);
_currentTask.UpdateStatus(MoveToPoint()); _currentTask.UpdateStatus(MoveToPoint());
break; break;
@@ -75,6 +94,7 @@ public class Player : MonoBehaviour
else else
{ {
AddTask(new PlayerTasks(Tasks.Move, _currentTask.TagretObject)); AddTask(new PlayerTasks(Tasks.Move, _currentTask.TagretObject));
AddTask(new PlayerTasks(Tasks.Rotate, _currentTask.TagretObject));
AddTask(_currentTask); AddTask(_currentTask);
_currentTask = null; _currentTask = null;
} }
@@ -98,25 +118,31 @@ public class Player : MonoBehaviour
} }
private bool pathComplete(Vector3 destination) 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) return false;
{
Quaternion rotation = Quaternion.LookRotation(target);
transform.rotation = rotation;
} }
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) private TaskStatus InteractWithObject(BaseInteractableObject interactableObject)
{ {
@@ -126,8 +152,11 @@ public class Player : MonoBehaviour
public void SetPlayerState(ActionStates newState) public void SetPlayerState(ActionStates newState)
{ {
if(newState== _currentState) if (newState == _currentState)
{ return; } {
return;
}
_isInAnimation = true;
switch (newState) switch (newState)
{ {
case ActionStates.Idle: case ActionStates.Idle:
@@ -149,6 +178,7 @@ public class Player : MonoBehaviour
_currentState = newState; _currentState = newState;
} }
bool isAnimationStatePlaying(int animLayer, string stateName) bool isAnimationStatePlaying(int animLayer, string stateName)
{ {
if (_animator.GetCurrentAnimatorStateInfo(animLayer).IsName(stateName) && if (_animator.GetCurrentAnimatorStateInfo(animLayer).IsName(stateName) &&