浏览代码

Added pathway NPC movement strategy

/main
Yohann Puyhaubert 4 年前
当前提交
bf1a236b
共有 39 个文件被更改,包括 578 次插入92 次删除
  1. 17
      UOP1_Project/Assets/ScriptableObjects/StateMachine/Config/NPCNotMoving.asset
  2. 8
      UOP1_Project/Assets/ScriptableObjects/StateMachine/Config/NPCNotMoving.asset.meta
  3. 17
      UOP1_Project/Assets/ScriptableObjects/StateMachine/Config/NPCPathwayConfig.asset
  4. 8
      UOP1_Project/Assets/ScriptableObjects/StateMachine/Config/NPCPathwayConfig.asset.meta
  5. 19
      UOP1_Project/Assets/ScriptableObjects/StateMachine/Config/NPCRoamingAroundCenter.asset
  6. 8
      UOP1_Project/Assets/ScriptableObjects/StateMachine/Config/NPCRoamingAroundCenter.asset.meta
  7. 15
      UOP1_Project/Assets/ScriptableObjects/StateMachine/Config/SlimeCritterEventChannel.asset
  8. 8
      UOP1_Project/Assets/ScriptableObjects/StateMachine/Config/SlimeCritterEventChannel.asset.meta
  9. 14
      UOP1_Project/Assets/ScriptableObjects/StateMachine/Critters/SlimeRockCritter/Actions/NPCMoveToNextDestination.asset
  10. 8
      UOP1_Project/Assets/ScriptableObjects/StateMachine/Critters/SlimeRockCritter/Actions/NPCMoveToNextDestination.asset.meta
  11. 14
      UOP1_Project/Assets/ScriptableObjects/StateMachine/Critters/SlimeRockCritter/Conditions/NPCMovementStopElapsed.asset
  12. 8
      UOP1_Project/Assets/ScriptableObjects/StateMachine/Critters/SlimeRockCritter/Conditions/NPCMovementStopElapsed.asset.meta
  13. 15
      UOP1_Project/Assets/Scripts/Characters/Config/NPCMovementConfigSO.cs
  14. 11
      UOP1_Project/Assets/Scripts/Characters/Config/NPCMovementConfigSO.cs.meta
  15. 12
      UOP1_Project/Assets/Scripts/Characters/Config/PathwayConfigSO.cs
  16. 11
      UOP1_Project/Assets/Scripts/Characters/Config/PathwayConfigSO.cs.meta
  17. 19
      UOP1_Project/Assets/Scripts/Characters/Config/RoamingAroundCenterConfigSO.cs
  18. 11
      UOP1_Project/Assets/Scripts/Characters/Config/RoamingAroundCenterConfigSO.cs.meta
  19. 23
      UOP1_Project/Assets/Scripts/Characters/NPCMovement.cs
  20. 11
      UOP1_Project/Assets/Scripts/Characters/NPCMovement.cs.meta
  21. 8
      UOP1_Project/Assets/Scripts/Characters/StateMachine/Actions/MovementActions.meta
  22. 62
      UOP1_Project/Assets/Scripts/Characters/StateMachine/Actions/NPCMoveToNextDestinationSO.cs
  23. 11
      UOP1_Project/Assets/Scripts/Characters/StateMachine/Actions/NPCMoveToNextDestinationSO.cs.meta
  24. 27
      UOP1_Project/Assets/Scripts/Characters/StateMachine/Conditions/NPCMovementStopConditionSO.cs
  25. 11
      UOP1_Project/Assets/Scripts/Characters/StateMachine/Conditions/NPCMovementStopConditionSO.cs.meta
  26. 18
      UOP1_Project/Assets/Scripts/Events/NPCMovementSwitcher.cs
  27. 11
      UOP1_Project/Assets/Scripts/Events/NPCMovementSwitcher.cs.meta
  28. 18
      UOP1_Project/Assets/Scripts/Events/ScriptableObjects/NPCMovementEventChannelSO.cs
  29. 11
      UOP1_Project/Assets/Scripts/Events/ScriptableObjects/NPCMovementEventChannelSO.cs.meta
  30. 11
      UOP1_Project/Assets/Scripts/Characters/StateMachine/Actions/MovementActions/NPCMovementAction.cs
  31. 11
      UOP1_Project/Assets/Scripts/Characters/StateMachine/Actions/MovementActions/NPCMovementAction.cs.meta
  32. 53
      UOP1_Project/Assets/Scripts/Characters/StateMachine/Actions/MovementActions/PathwayMovementAction.cs
  33. 11
      UOP1_Project/Assets/Scripts/Characters/StateMachine/Actions/MovementActions/PathwayMovementAction.cs.meta
  34. 58
      UOP1_Project/Assets/Scripts/Characters/StateMachine/Actions/MovementActions/RoamingMovementAction.cs
  35. 16
      UOP1_Project/Assets/ScriptableObjects/StateMachine/Critters/SlimeRockCritter/Actions/RoamingAroundSpawningPositionAction.asset
  36. 8
      UOP1_Project/Assets/ScriptableObjects/StateMachine/Critters/SlimeRockCritter/Actions/RoamingAroundSpawningPositionAction.asset.meta
  37. 68
      UOP1_Project/Assets/Scripts/Characters/StateMachine/Actions/RoamingAroundSpawningPositionActionSO.cs
  38. 0
      /UOP1_Project/Assets/Scripts/Characters/StateMachine/Actions/MovementActions/RoamingMovementAction.cs.meta

17
UOP1_Project/Assets/ScriptableObjects/StateMachine/Config/NPCNotMoving.asset


%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &11400000
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 76542500a512494489526e9bcfd3a1b8, type: 3}
m_Name: NPCNotMoving
m_EditorClassIdentifier:
_stopDuration: 0
_speed: 0
_waypoints: []

8
UOP1_Project/Assets/ScriptableObjects/StateMachine/Config/NPCNotMoving.asset.meta


fileFormatVersion: 2
guid: cee031d3f51dfd24880fa8842d93af83
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 11400000
userData:
assetBundleName:
assetBundleVariant:

17
UOP1_Project/Assets/ScriptableObjects/StateMachine/Config/NPCPathwayConfig.asset


%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &11400000
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 76542500a512494489526e9bcfd3a1b8, type: 3}
m_Name: PathwayConfig
m_EditorClassIdentifier:
_waypoints: []
_stopDuration: 0
_speed: 0

8
UOP1_Project/Assets/ScriptableObjects/StateMachine/Config/NPCPathwayConfig.asset.meta


fileFormatVersion: 2
guid: 3fd89e57021cbbe41a7c94dbf80a103e
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 11400000
userData:
assetBundleName:
assetBundleVariant:

19
UOP1_Project/Assets/ScriptableObjects/StateMachine/Config/NPCRoamingAroundCenter.asset


%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &11400000
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 2724eed9fc9a3904b9edc8180dace7f2, type: 3}
m_Name: RoamingAroundCenter
m_EditorClassIdentifier:
_fromSpawningPoint: 1
_customCenter: {x: 0, y: 0, z: 0}
_radius: 0
_speed: 0
_stopDuration: 0

8
UOP1_Project/Assets/ScriptableObjects/StateMachine/Config/NPCRoamingAroundCenter.asset.meta


fileFormatVersion: 2
guid: a6f83ee8260ad6443be926803deffbfe
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 11400000
userData:
assetBundleName:
assetBundleVariant:

15
UOP1_Project/Assets/ScriptableObjects/StateMachine/Config/SlimeCritterEventChannel.asset


%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &11400000
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 0c96b3d50eb64e741a7219a67ca93bdf, type: 3}
m_Name: SlimeCritterEventChannel
m_EditorClassIdentifier:
description:

8
UOP1_Project/Assets/ScriptableObjects/StateMachine/Config/SlimeCritterEventChannel.asset.meta


fileFormatVersion: 2
guid: 31c59dd3b3adb4249aaf331b7ea4a11b
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 11400000
userData:
assetBundleName:
assetBundleVariant:

14
UOP1_Project/Assets/ScriptableObjects/StateMachine/Critters/SlimeRockCritter/Actions/NPCMoveToNextDestination.asset


%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &11400000
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 8e27de7dbba554640996a6eaf8778f75, type: 3}
m_Name: NPCMoveToNextDestination
m_EditorClassIdentifier:

8
UOP1_Project/Assets/ScriptableObjects/StateMachine/Critters/SlimeRockCritter/Actions/NPCMoveToNextDestination.asset.meta


fileFormatVersion: 2
guid: 976208a7240935042a5f8bbcb0d35914
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 11400000
userData:
assetBundleName:
assetBundleVariant:

14
UOP1_Project/Assets/ScriptableObjects/StateMachine/Critters/SlimeRockCritter/Conditions/NPCMovementStopElapsed.asset


%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &11400000
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 8dbbed255c260fd419d891a9bf6025cb, type: 3}
m_Name: NPCMovementStopElapsed
m_EditorClassIdentifier:

8
UOP1_Project/Assets/ScriptableObjects/StateMachine/Critters/SlimeRockCritter/Conditions/NPCMovementStopElapsed.asset.meta


fileFormatVersion: 2
guid: 7bcf4f38958f2d34b97fee183d1d7c25
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 11400000
userData:
assetBundleName:
assetBundleVariant:

15
UOP1_Project/Assets/Scripts/Characters/Config/NPCMovementConfigSO.cs


using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Localization;
public class NPCMovementConfigSO : ScriptableObject
{
[Tooltip("Waypoint stop duration")]
[SerializeField] private float _stopDuration;
[Tooltip("Roaming speed")]
[SerializeField] private float _speed;
public float Speed => _speed;
public float StopDuration => _stopDuration;
}

11
UOP1_Project/Assets/Scripts/Characters/Config/NPCMovementConfigSO.cs.meta


fileFormatVersion: 2
guid: fad8fe75bf5751e40beeaaed145a73d6
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

12
UOP1_Project/Assets/Scripts/Characters/Config/PathwayConfigSO.cs


using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu(fileName = "PathwayConfig", menuName = "EntityConfig/Pathway Config")]
public class PathwayConfigSO : NPCMovementConfigSO
{
[Tooltip("Pathway waypoints")]
[SerializeField] private List<Vector3> _waypoints;
public List<Vector3> Waypoints => _waypoints;
}

11
UOP1_Project/Assets/Scripts/Characters/Config/PathwayConfigSO.cs.meta


fileFormatVersion: 2
guid: 76542500a512494489526e9bcfd3a1b8
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

19
UOP1_Project/Assets/Scripts/Characters/Config/RoamingAroundCenterConfigSO.cs


using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu(fileName = "RoamingAroundCenter", menuName = "EntityConfig/Roaming Around Center")]
public class RoamingAroundCenterConfigSO : NPCMovementConfigSO
{
[Tooltip("Is roaming from spwaning center")]
[SerializeField] private bool _fromSpawningPoint = true;
[Tooltip("Custom roaming center")]
[SerializeField] private Vector3 _customCenter;
[Tooltip("Roaming distance from center")]
[SerializeField] private float _radius;
public bool FromSpawningPoint => _fromSpawningPoint;
public Vector3 CustomCenter => _customCenter;
public float Radius => _radius;
}

11
UOP1_Project/Assets/Scripts/Characters/Config/RoamingAroundCenterConfigSO.cs.meta


fileFormatVersion: 2
guid: 2724eed9fc9a3904b9edc8180dace7f2
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

23
UOP1_Project/Assets/Scripts/Characters/NPCMovement.cs


using UnityEngine;
using System.Collections;
public class NPCMovement : MonoBehaviour
{
[SerializeField] NPCMovementConfigSO _npcMovementConfig;
[SerializeField] NPCMovementEventChannelSO _channel;
public NPCMovementConfigSO NPCMovementConfig => _npcMovementConfig;
private void OnEnable()
{
if (_channel != null)
_channel.OnEventRaised += Respond;
}
private void Respond(NPCMovementConfigSO value)
{
_npcMovementConfig = value;
}
}

11
UOP1_Project/Assets/Scripts/Characters/NPCMovement.cs.meta


fileFormatVersion: 2
guid: ac26da34fd58af544a84bf1977bcc424
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

8
UOP1_Project/Assets/Scripts/Characters/StateMachine/Actions/MovementActions.meta


fileFormatVersion: 2
guid: 2ff4ff1b3350ade419a938ef8cc26aa0
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

62
UOP1_Project/Assets/Scripts/Characters/StateMachine/Actions/NPCMoveToNextDestinationSO.cs


using UnityEngine;
using UnityEngine.AI;
using UOP1.StateMachine;
using UOP1.StateMachine.ScriptableObjects;
[CreateAssetMenu(fileName = "NPCMoveToNextDestination", menuName = "State Machines/Actions/NPC Move To Next Destination")]
public class NPCMoveToNextDestinationSO : StateActionSO
{
protected override StateAction CreateAction() => new NPCMoveToNextDestination();
}
public class NPCMoveToNextDestination : StateAction
{
private NPCMovement _npcMovement;
private NPCMovementConfigSO _config;
private NPCMovementAction _action;
private NavMeshAgent _agent;
public override void Awake(StateMachine stateMachine)
{
_agent = stateMachine.GetComponent<NavMeshAgent>();
_npcMovement = stateMachine.GetComponent<NPCMovement>();
InitMovementStrategy(_npcMovement.NPCMovementConfig);
}
public override void OnStateEnter()
{
if (_config != _npcMovement.NPCMovementConfig)
{
InitMovementStrategy(_npcMovement.NPCMovementConfig);
}
_action.OnStateEnter();
}
public override void OnUpdate()
{
_action.OnUpdate();
}
public override void OnStateExit()
{
_action.OnStateExit();
}
private void InitMovementStrategy(NPCMovementConfigSO config)
{
_config = config;
if (_npcMovement.NPCMovementConfig is RoamingAroundCenterConfigSO)
{
_action = new RoamingMovementAction(
(RoamingAroundCenterConfigSO)_npcMovement.NPCMovementConfig,
_agent,
_npcMovement.transform.position);
}
else if (_npcMovement.NPCMovementConfig is PathwayConfigSO)
{
_action = new PathwayMovementAction(
(PathwayConfigSO)_npcMovement.NPCMovementConfig,
_agent);
}
}
}

11
UOP1_Project/Assets/Scripts/Characters/StateMachine/Actions/NPCMoveToNextDestinationSO.cs.meta


fileFormatVersion: 2
guid: 8e27de7dbba554640996a6eaf8778f75
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

27
UOP1_Project/Assets/Scripts/Characters/StateMachine/Conditions/NPCMovementStopConditionSO.cs


using UnityEngine;
using UOP1.StateMachine;
using UOP1.StateMachine.ScriptableObjects;
[CreateAssetMenu(menuName = "State Machines/Conditions/NPC Movement Stop Elapsed")]
public class NPCMovementStopConditionSO : StateConditionSO<NPCMovementStopCondition>
{
}
public class NPCMovementStopCondition : Condition
{
private float _startTime;
private NPCMovement _npcMovement;
public override void Awake(StateMachine stateMachine)
{
_npcMovement = stateMachine.GetComponent<NPCMovement>();
}
public override void OnStateEnter()
{
_startTime = Time.time;
}
protected override bool Statement() => Time.time >= _startTime + _npcMovement.NPCMovementConfig.StopDuration;
}

11
UOP1_Project/Assets/Scripts/Characters/StateMachine/Conditions/NPCMovementStopConditionSO.cs.meta


fileFormatVersion: 2
guid: 8dbbed255c260fd419d891a9bf6025cb
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

18
UOP1_Project/Assets/Scripts/Events/NPCMovementSwitcher.cs


using UnityEngine;
using System.Collections;
public class NPCMovementSwitcher : MonoBehaviour
{
[SerializeField] private NPCMovementEventChannelSO _movementChannel;
[SerializeField] private NPCMovementConfigSO _movementConfig;
[ContextMenu("Trigger NPC Movement switch")]
public void SwitchMovement()
{
if (_movementChannel != null && _movementConfig != null)
{
_movementChannel.RaiseEvent(_movementConfig);
}
}
}

11
UOP1_Project/Assets/Scripts/Events/NPCMovementSwitcher.cs.meta


fileFormatVersion: 2
guid: 0ef2f17bbd66703429f4a40c667749ee
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

18
UOP1_Project/Assets/Scripts/Events/ScriptableObjects/NPCMovementEventChannelSO.cs


using UnityEngine.Events;
using UnityEngine;
/// <summary>
/// This class is used for Events that have one NPCMovementConfigSO argument.
/// </summary>
[CreateAssetMenu(menuName = "Events/NPC Movement Event Channel")]
public class NPCMovementEventChannelSO : EventChannelBaseSO
{
public UnityAction<NPCMovementConfigSO> OnEventRaised;
public void RaiseEvent(NPCMovementConfigSO value)
{
if (OnEventRaised != null)
OnEventRaised.Invoke(value);
}
}

11
UOP1_Project/Assets/Scripts/Events/ScriptableObjects/NPCMovementEventChannelSO.cs.meta


fileFormatVersion: 2
guid: 0c96b3d50eb64e741a7219a67ca93bdf
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

11
UOP1_Project/Assets/Scripts/Characters/StateMachine/Actions/MovementActions/NPCMovementAction.cs


using UnityEngine;
using System.Collections;
public abstract class NPCMovementAction
{
public abstract void OnUpdate();
public abstract void OnStateEnter();
public abstract void OnStateExit();
}

11
UOP1_Project/Assets/Scripts/Characters/StateMachine/Actions/MovementActions/NPCMovementAction.cs.meta


fileFormatVersion: 2
guid: 9926defed0c86824386575e4f5d53739
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

53
UOP1_Project/Assets/Scripts/Characters/StateMachine/Actions/MovementActions/PathwayMovementAction.cs


using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class PathwayMovementAction : NPCMovementAction
{
private NavMeshAgent _agent;
private bool _isActiveAgent;
private List<Vector3> _wayppoints;
private int _wayPointIndex;
private float _roamingSpeed;
public PathwayMovementAction(
PathwayConfigSO config, NavMeshAgent agent)
{
_agent = agent;
_isActiveAgent = _agent != null && _agent.isActiveAndEnabled && _agent.isOnNavMesh;
_wayPointIndex = 0;
_roamingSpeed = config.Speed;
_wayppoints = config.Waypoints;
}
public override void OnUpdate()
{
}
public override void OnStateEnter()
{
if (_isActiveAgent)
{
_agent.speed = _roamingSpeed;
_agent.isStopped = false;
_agent.SetDestination(GetNextDestination());
}
}
public override void OnStateExit()
{
}
private Vector3 GetNextDestination()
{
Vector3 result = _agent.transform.position;
if (_wayppoints.Count > 0)
{
_wayPointIndex = (_wayPointIndex + 1) % _wayppoints.Count;
result = _wayppoints[_wayPointIndex];
}
return result;
}
}

11
UOP1_Project/Assets/Scripts/Characters/StateMachine/Actions/MovementActions/PathwayMovementAction.cs.meta


fileFormatVersion: 2
guid: f2a3512884f9b254da6dc1255fbcfd50
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

58
UOP1_Project/Assets/Scripts/Characters/StateMachine/Actions/MovementActions/RoamingMovementAction.cs


using UnityEngine;
using UnityEngine.AI;
public class RoamingMovementAction : NPCMovementAction
{
private NavMeshAgent _agent;
private bool _isActiveAgent;
private Vector3 _startPosition;
private float _roamingSpeed;
private float _roamingDistance;
private Vector3 _roamingTargetPosition;
public RoamingMovementAction(
RoamingAroundCenterConfigSO config, NavMeshAgent agent, Vector3 startPosition)
{
_agent = agent;
_isActiveAgent = _agent != null && _agent.isActiveAndEnabled && _agent.isOnNavMesh;
if (config.FromSpawningPoint)
{
_startPosition = startPosition;
}
else
{
_startPosition = config.CustomCenter;
}
_roamingSpeed = config.Speed;
_roamingDistance = config.Radius;
}
public override void OnUpdate()
{
}
public override void OnStateEnter()
{
if (_isActiveAgent)
{
_roamingTargetPosition = GetRoamingPositionAroundPosition(_startPosition);
_agent.speed = _roamingSpeed;
_agent.isStopped = false;
_agent.SetDestination(_roamingTargetPosition);
}
}
public override void OnStateExit()
{
}
// Compute a random target position around the starting position.
private Vector3 GetRoamingPositionAroundPosition(Vector3 position)
{
return position + new Vector3(Random.Range(-1, 1), 0.0f, Random.Range(-1, 1)).normalized * Random.Range(_roamingDistance / 2, _roamingDistance);
}
}

16
UOP1_Project/Assets/ScriptableObjects/StateMachine/Critters/SlimeRockCritter/Actions/RoamingAroundSpawningPositionAction.asset


%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &11400000
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: a65678f74cbc33e45bf1e019ca88652e, type: 3}
m_Name: RoamingAroundSpawningPositionAction
m_EditorClassIdentifier:
_roamingSpeed: 1
_roamingDistance: 10

8
UOP1_Project/Assets/ScriptableObjects/StateMachine/Critters/SlimeRockCritter/Actions/RoamingAroundSpawningPositionAction.asset.meta


fileFormatVersion: 2
guid: 768de7a24359ece438c9e7cdb6f9670e
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 0
userData:
assetBundleName:
assetBundleVariant:

68
UOP1_Project/Assets/Scripts/Characters/StateMachine/Actions/RoamingAroundSpawningPositionActionSO.cs


using UnityEngine;
using UnityEngine.AI;
using UOP1.StateMachine;
using UOP1.StateMachine.ScriptableObjects;
[CreateAssetMenu(fileName = "RoamingAroundSpawningPositionAction", menuName = "State Machines/Actions/Roaming Around Spawning Position Action")]
public class RoamingAroundSpawningPositionActionSO : StateActionSO
{
[Tooltip("NPC roaming speed.")]
[SerializeField] private float _roamingSpeed = default;
[Tooltip("How far the NPC can roam around its spawning point.")]
[SerializeField] private float _roamingDistance = default;
public float RoamingSpeed => _roamingSpeed;
public float RoamingDistance => _roamingDistance;
protected override StateAction CreateAction() => new RoamingAroundSpawningPositionAction();
}
public class RoamingAroundSpawningPositionAction : StateAction
{
private NavMeshAgent _agent;
private bool _isActiveAgent;
private Vector3 _startPosition;
private float _roamingSpeed;
private float _roamingDistance;
private Vector3 _roamingTargetPosition;
public override void Awake(StateMachine stateMachine)
{
RoamingAroundSpawningPositionActionSO config = (RoamingAroundSpawningPositionActionSO)OriginSO;
_agent = stateMachine.gameObject.GetComponent<NavMeshAgent>();
_isActiveAgent = _agent != null && _agent.isActiveAndEnabled && _agent.isOnNavMesh;
_startPosition = stateMachine.gameObject.transform.position;
_roamingSpeed = config.RoamingSpeed;
_roamingDistance = config.RoamingDistance;
}
public override void OnUpdate()
{
}
public override void OnStateEnter()
{
if (_isActiveAgent)
{
_roamingTargetPosition = GetRoamingPositionAroundPosition(_startPosition);
_agent.speed = _roamingSpeed;
_agent.isStopped = false;
_agent.SetDestination(_roamingTargetPosition);
}
}
public override void OnStateExit()
{
}
// Compute a random target position around the starting position.
private Vector3 GetRoamingPositionAroundPosition(Vector3 position)
{
return position + new Vector3(Random.Range(-1, 1), 0.0f, Random.Range(-1, 1)).normalized * Random.Range(_roamingDistance / 2, _roamingDistance);
}
}

/UOP1_Project/Assets/Scripts/Characters/StateMachine/Actions/RoamingAroundSpawningPositionActionSO.cs.meta → /UOP1_Project/Assets/Scripts/Characters/StateMachine/Actions/MovementActions/RoamingMovementAction.cs.meta

正在加载...
取消
保存