Skip to content
Snippets Groups Projects
Commit f49dc49c authored by Karel Ševčík's avatar Karel Ševčík
Browse files

Improve Rabbit AI

parent 219bd356
No related branches found
No related tags found
No related merge requests found
...@@ -3054,7 +3054,7 @@ Transform: ...@@ -3054,7 +3054,7 @@ Transform:
m_PrefabAsset: {fileID: 0} m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 461669300} m_GameObject: {fileID: 461669300}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: -18.21, y: 12.394, z: -14.01} m_LocalPosition: {x: -10.54, y: 15.894, z: 32.63}
m_LocalScale: {x: 0.25, y: 0.25, z: 0.25} m_LocalScale: {x: 0.25, y: 0.25, z: 0.25}
m_Children: [] m_Children: []
m_Father: {fileID: 0} m_Father: {fileID: 0}
...@@ -3099,6 +3099,12 @@ MonoBehaviour: ...@@ -3099,6 +3099,12 @@ MonoBehaviour:
FleeSpeed: 5 FleeSpeed: 5
HidingSpots: HidingSpots:
- {fileID: 466511600} - {fileID: 466511600}
- {fileID: 242343348}
- {fileID: 181470655}
- {fileID: 1142502979}
- {fileID: 1787846885}
- {fileID: 63741814}
- {fileID: 1608465880}
Agent: {fileID: 461669305} Agent: {fileID: 461669305}
--- !u!1001 &465783321 --- !u!1001 &465783321
PrefabInstance: PrefabInstance:
...@@ -8050,6 +8056,88 @@ Transform: ...@@ -8050,6 +8056,88 @@ Transform:
m_CorrespondingSourceObject: {fileID: 2198473111904891261, guid: a641e6842beaa0f4d98eea8f2892caa0, type: 3} m_CorrespondingSourceObject: {fileID: 2198473111904891261, guid: a641e6842beaa0f4d98eea8f2892caa0, type: 3}
m_PrefabInstance: {fileID: 1605046071} m_PrefabInstance: {fileID: 1605046071}
m_PrefabAsset: {fileID: 0} m_PrefabAsset: {fileID: 0}
--- !u!1 &1608465879
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 1608465880}
- component: {fileID: 1608465882}
- component: {fileID: 1608465881}
m_Layer: 0
m_Name: Shrub (41)
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &1608465880
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1608465879}
m_LocalRotation: {x: -0.7071068, y: 0, z: -0, w: 0.7071067}
m_LocalPosition: {x: -65.15, y: 0.39, z: 96.1}
m_LocalScale: {x: 0.36732757, y: 0.3140628, z: 0.26565963}
m_Children: []
m_Father: {fileID: 4571592687677348262}
m_RootOrder: 41
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!23 &1608465881
MeshRenderer:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1608465879}
m_Enabled: 1
m_CastShadows: 1
m_ReceiveShadows: 1
m_DynamicOccludee: 1
m_MotionVectors: 1
m_LightProbeUsage: 1
m_ReflectionProbeUsage: 1
m_RayTracingMode: 2
m_RayTraceProcedural: 0
m_RenderingLayerMask: 1
m_RendererPriority: 0
m_Materials:
- {fileID: 2100000, guid: bc41aa93d94217b44b693af98b364e90, type: 2}
- {fileID: 2100000, guid: 42f6a10f2263e424aa65c8523bbe8e28, type: 2}
m_StaticBatchInfo:
firstSubMesh: 0
subMeshCount: 0
m_StaticBatchRoot: {fileID: 0}
m_ProbeAnchor: {fileID: 0}
m_LightProbeVolumeOverride: {fileID: 0}
m_ScaleInLightmap: 1
m_ReceiveGI: 1
m_PreserveUVs: 0
m_IgnoreNormalsForChartDetection: 0
m_ImportantGI: 0
m_StitchLightmapSeams: 1
m_SelectedEditorRenderState: 3
m_MinimumChartSize: 4
m_AutoUVMaxDistance: 0.5
m_AutoUVMaxAngle: 89
m_LightmapParameters: {fileID: 0}
m_SortingLayerID: 0
m_SortingLayer: 0
m_SortingOrder: 0
m_AdditionalVertexStreams: {fileID: 0}
--- !u!33 &1608465882
MeshFilter:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1608465879}
m_Mesh: {fileID: -8007423518850538223, guid: 58282512c6ad5a7469a551ac36f87ef6, type: 3}
--- !u!1001 &1613784635 --- !u!1001 &1613784635
PrefabInstance: PrefabInstance:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
...@@ -22675,6 +22763,7 @@ Transform: ...@@ -22675,6 +22763,7 @@ Transform:
- {fileID: 1270380777} - {fileID: 1270380777}
- {fileID: 1454681096} - {fileID: 1454681096}
- {fileID: 1639598964} - {fileID: 1639598964}
- {fileID: 1608465880}
m_Father: {fileID: 2029239812} m_Father: {fileID: 2029239812}
m_RootOrder: 2 m_RootOrder: 2
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
......
using UnityEngine; using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.AI; using UnityEngine.AI;
   
namespace Nudle.Scripts.AI namespace Nudle.Scripts.AI
...@@ -25,6 +27,7 @@ namespace Nudle.Scripts.AI ...@@ -25,6 +27,7 @@ namespace Nudle.Scripts.AI
{ {
if (!Fleeing) if (!Fleeing)
{ {
Agent.speed = WalkSpeed;
if (Timer <= 0f) if (Timer <= 0f)
{ {
Agent.SetDestination(FindRandomTarget()); Agent.SetDestination(FindRandomTarget());
...@@ -50,9 +53,8 @@ namespace Nudle.Scripts.AI ...@@ -50,9 +53,8 @@ namespace Nudle.Scripts.AI
private Vector3 FindRandomTarget() private Vector3 FindRandomTarget()
{ {
var x = transform.position.x + Random.Range(-DetectionRadius, DetectionRadius); var x = transform.position.x + Random.Range(-DetectionRadius, DetectionRadius);
var z = transform.position.x + Random.Range(-DetectionRadius, DetectionRadius); var z = transform.position.z + Random.Range(-DetectionRadius, DetectionRadius);
var y = Terrain.activeTerrain.SampleHeight(new Vector3(x, 0, z)); var y = Terrain.activeTerrain.SampleHeight(new Vector3(x, 0, z));
Debug.Log($"RABBIT MOVE TO {x}, {y}, {z}!");
return new Vector3(x, y, z); return new Vector3(x, y, z);
} }
   
...@@ -60,11 +62,45 @@ namespace Nudle.Scripts.AI ...@@ -60,11 +62,45 @@ namespace Nudle.Scripts.AI
   
private void Flee() private void Flee()
{ {
// BIG MATH var hidingSpot = FindHidingSpot();
Agent.SetDestination(HidingSpots[0].position); if (hidingSpot == default)
hidingSpot = HidingSpots[Random.Range(0, HidingSpots.Length)].position;
Agent.SetDestination(hidingSpot);
Agent.speed = FleeSpeed;
Fleeing = true; Fleeing = true;
} }
   
private Vector3 FindHidingSpot()
=> GetValidHidingSpots().OrderBy(pos => Vector3.Distance(pos, transform.position)).FirstOrDefault();
private IEnumerable<Vector3> GetValidHidingSpots()
{
var idealDirection = (transform.position - GetPlayerPosition()).normalized;
return HidingSpots.Where(spot => Vector3.Angle(idealDirection, spot.position - transform.position) < 90f)
.Select(t => t.position);
}
private Vector3 GetPlayerPosition() => GameObject.FindGameObjectWithTag("Player").transform.position; private Vector3 GetPlayerPosition() => GameObject.FindGameObjectWithTag("Player").transform.position;
internal void OnDrawGizmosSelected()
{
if (!Fleeing)
{
Gizmos.color = Color.green;
Gizmos.DrawWireSphere(transform.position, DetectionRadius);
Gizmos.color = Color.yellow;
Gizmos.DrawLine(transform.position, Agent.destination);
foreach (var spot in GetValidHidingSpots())
{
Gizmos.color = Color.blue;
Gizmos.DrawLine(transform.position, spot);
}
}
else
{
Gizmos.color = Color.red;
Gizmos.DrawLine(transform.position, Agent.destination);
}
}
} }
} }
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment