HandleMovement + Basic NavMesh

This commit is contained in:
Crizomb 2024-12-23 01:32:01 +01:00
parent d4a32d9593
commit 47a59a5440
18 changed files with 3104 additions and 52 deletions

Binary file not shown.

View file

@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: b0a06a57645516475b4d25eda6f77db7
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,63 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!850595691 &4890085278179872738
LightingSettings:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name: New Lighting Settings
serializedVersion: 9
m_EnableBakedLightmaps: 1
m_EnableRealtimeLightmaps: 0
m_RealtimeEnvironmentLighting: 1
m_BounceScale: 1
m_AlbedoBoost: 1
m_IndirectOutputScale: 1
m_UsingShadowmask: 1
m_BakeBackend: 1
m_LightmapMaxSize: 1024
m_LightmapSizeFixed: 0
m_UseMipmapLimits: 1
m_BakeResolution: 40
m_Padding: 2
m_LightmapCompression: 3
m_AO: 0
m_AOMaxDistance: 1
m_CompAOExponent: 1
m_CompAOExponentDirect: 0
m_ExtractAO: 0
m_MixedBakeMode: 2
m_LightmapsBakeMode: 1
m_FilterMode: 1
m_LightmapParameters: {fileID: 15204, guid: 0000000000000000f000000000000000, type: 0}
m_ExportTrainingData: 0
m_EnableWorkerProcessBaking: 1
m_TrainingDataDestination: TrainingData
m_RealtimeResolution: 2
m_ForceWhiteAlbedo: 0
m_ForceUpdates: 0
m_PVRCulling: 1
m_PVRSampling: 1
m_PVRDirectSampleCount: 32
m_PVRSampleCount: 512
m_PVREnvironmentSampleCount: 256
m_PVREnvironmentReferencePointCount: 2048
m_LightProbeSampleCountMultiplier: 4
m_PVRBounces: 2
m_PVRMinBounces: 2
m_PVREnvironmentImportanceSampling: 1
m_PVRFilteringMode: 1
m_PVRDenoiserTypeDirect: 1
m_PVRDenoiserTypeIndirect: 1
m_PVRDenoiserTypeAO: 1
m_PVRFilterTypeDirect: 0
m_PVRFilterTypeIndirect: 0
m_PVRFilterTypeAO: 0
m_PVRFilteringGaussRadiusDirect: 1
m_PVRFilteringGaussRadiusIndirect: 1
m_PVRFilteringGaussRadiusAO: 1
m_PVRFilteringAtrousPositionSigmaDirect: 0.5
m_PVRFilteringAtrousPositionSigmaIndirect: 2
m_PVRFilteringAtrousPositionSigmaAO: 1
m_RespectSceneVisibilityWhenBakingGI: 0

View file

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 45e7c6b3350d2da8fbcc194491f502eb
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 4890085278179872738
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 88bf7e30ad2b4ff23aa29f73659d5b7d
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

File diff suppressed because it is too large Load diff

Binary file not shown.

View file

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 5e3a1e10e8e69f53aa9561702b0b9803
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 23800000
userData:
assetBundleName:
assetBundleVariant:

View file

@ -2,6 +2,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
[RequireComponent(typeof(Unit))]
public class AttackHandler : MonoBehaviour public class AttackHandler : MonoBehaviour
{ {
[SerializeField] private float damage; [SerializeField] private float damage;
@ -10,6 +11,12 @@ public class AttackHandler : MonoBehaviour
[SerializeField] private float knockback; [SerializeField] private float knockback;
private float _timer; private float _timer;
private Unit _unit;
void Awake()
{
_unit = GetComponent<Unit>();
}
void Start() void Start()
{ {
@ -23,7 +30,7 @@ public class AttackHandler : MonoBehaviour
/// <summary> /// <summary>
/// Launch an Attack, and return true if it's possible to attack /// Launch an Attack, and return true if it's possible to attack
/// see what Units are in the attackShape, apply damage and knockback to those unit /// see what Units are in the attackShape, apply damage and knockback to those unit if they're ennemies
/// </summary> /// </summary>
public bool Attack() public bool Attack()
{ {
@ -35,6 +42,8 @@ public class AttackHandler : MonoBehaviour
if (!target.CompareTag("Unit")) continue; if (!target.CompareTag("Unit")) continue;
// GetComponent is expensive in performance, optimize here if it's slow // GetComponent is expensive in performance, optimize here if it's slow
Unit unit = target.GetComponent<Unit>(); Unit unit = target.GetComponent<Unit>();
// No friendly fire
if (unit.IsTeamA == _unit.IsTeamA) continue;
unit.Health.TakeDamage(damage); unit.Health.TakeDamage(damage);
Vector3 knockbackVector = knockback * (target.transform.position - transform.position).normalized; Vector3 knockbackVector = knockback * (target.transform.position - transform.position).normalized;
unit.Body.AddForce(knockbackVector, ForceMode.Impulse); unit.Body.AddForce(knockbackVector, ForceMode.Impulse);

View file

@ -0,0 +1,10 @@
using System;
using System.Collections.Generic;
using UnityEngine;
public static class GlobalsVariable
{
public static List<Unit> AliveUnitsTeamA = new List<Unit>();
public static List<Unit> AliveUnitsTeamB = new List<Unit>();
}

View file

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: aff398f48f2c36187b8027e20ad9dc16

View file

@ -1,10 +1,18 @@
using UnityEngine; using UnityEngine;
[RequireComponent(typeof(Unit))]
public class HealthHandler : MonoBehaviour public class HealthHandler : MonoBehaviour
{ {
[SerializeField] private float maxHealth; [SerializeField] private float maxHealth;
[SerializeField] private float currentHealth; [SerializeField] private float currentHealth;
[SerializeField] private float armor; [SerializeField] private float armor;
private Unit _unit;
public void Awake()
{
_unit = GetComponent<Unit>();
}
public void TakeDamage(float damage) public void TakeDamage(float damage)
{ {
@ -39,6 +47,14 @@ public class HealthHandler : MonoBehaviour
public void Death() public void Death()
{ {
print("you dead"); print("you dead");
if (_unit.IsTeamA)
{
GlobalsVariable.AliveUnitsTeamA.Remove(_unit);
}
else
{
GlobalsVariable.AliveUnitsTeamB.Remove(_unit);
}
} }
} }

View file

@ -0,0 +1,65 @@
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.AI;
[RequireComponent(typeof(Unit))]
public class MovementHandler : MonoBehaviour
{
[SerializeField] private float speed;
[SerializeField] private NavMeshAgent agent;
[SerializeField] private Transform defaultMoveTarget;
private Unit _unit;
void Awake()
{
_unit = GetComponent<Unit>();
}
public void ChangeSpeed(float newSpeed)
{
speed = newSpeed;
}
public void StopMoving()
{
agent.speed = 0;
}
public void ResumeMoving()
{
agent.speed = speed;
}
public void MoveTowards(Vector3 destination)
{
agent.SetDestination(destination);
}
public void MoveTowardsNearest()
{
MoveTowards(FindNearestEnemy().transform.position);
}
Unit FindNearestEnemy()
{
List<Unit> enemies = _unit.IsTeamA ? GlobalsVariable.AliveUnitsTeamB : GlobalsVariable.AliveUnitsTeamA;
return enemies
.Aggregate((nearest, current) =>
(current.transform.position - transform.position).sqrMagnitude <
(nearest.transform.position - transform.position).sqrMagnitude
? current
: nearest);
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
//MoveTowards(defaultMoveTarget.position);
MoveTowardsNearest();
}
}
}

View file

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: aa245c9f3dff7f6b9888b009a1023628

View file

@ -1,19 +1,41 @@
using Unity.VisualScripting; using Unity.VisualScripting;
using UnityEngine; using UnityEngine;
using UnityEngine.AI;
[DisallowMultipleComponent]
[RequireComponent(typeof(Rigidbody))]
public class Unit : MonoBehaviour public class Unit : MonoBehaviour
{ {
[field: SerializeField] public Rigidbody Body { get; private set; } [field: SerializeField] public Rigidbody Body { get; private set; }
[field: SerializeField] public HealthHandler Health { get; private set; } [field: SerializeField] public HealthHandler Health { get; private set; }
[field: SerializeField] public AttackHandler Attack { get; private set; } [field: SerializeField] public AttackHandler Attack { get; private set; }
[field: SerializeField] public MovementHandler Move { get; private set; }
[field: SerializeField] public bool IsTeamA { get; private set; }
[SerializeField] private int price;
void Start() void OnValidate()
{ {
Debug.Assert(Body != null); Debug.Assert(Body != null);
Debug.Assert(Health != null); Debug.Assert(Health != null);
Debug.Assert(Attack != null); Debug.Assert(Attack != null);
Debug.Assert(Move != null);
}
void Awake()
{
if (IsTeamA)
{
GlobalsVariable.AliveUnitsTeamA.Add(this);
}
else
{
GlobalsVariable.AliveUnitsTeamB.Add(this);
}
} }

View file

@ -1,5 +1,6 @@
{ {
"dependencies": { "dependencies": {
"com.unity.ai.navigation": "2.0.5",
"com.unity.collab-proxy": "2.6.0", "com.unity.collab-proxy": "2.6.0",
"com.unity.feature.development": "1.0.2", "com.unity.feature.development": "1.0.2",
"com.unity.inputsystem": "1.11.2", "com.unity.inputsystem": "1.11.2",

View file

@ -1,5 +1,14 @@
{ {
"dependencies": { "dependencies": {
"com.unity.ai.navigation": {
"version": "2.0.5",
"depth": 0,
"source": "registry",
"dependencies": {
"com.unity.modules.ai": "1.0.0"
},
"url": "https://packages.unity.com"
},
"com.unity.collab-proxy": { "com.unity.collab-proxy": {
"version": "2.6.0", "version": "2.6.0",
"depth": 0, "depth": 0,

View file

@ -71,20 +71,22 @@ NavMeshProjectSettings:
cost: 1 cost: 1
m_LastAgentTypeID: -887442657 m_LastAgentTypeID: -887442657
m_Settings: m_Settings:
- serializedVersion: 2 - serializedVersion: 3
agentTypeID: 0 agentTypeID: 0
agentRadius: 0.5 agentRadius: 0.5
agentHeight: 2 agentHeight: 2
agentSlope: 45 agentSlope: 45
agentClimb: 0.75 agentClimb: 0.75
ledgeDropHeight: 0 ledgeDropHeight: 10
maxJumpAcrossDistance: 0 maxJumpAcrossDistance: 10
minRegionArea: 2 minRegionArea: 2
manualCellSize: 0 manualCellSize: 0
cellSize: 0.16666667 cellSize: 0.16666667
manualTileSize: 0 manualTileSize: 0
tileSize: 256 tileSize: 256
accuratePlacement: 0 buildHeightMesh: 0
maxJobWorkers: 0
preserveTilesOutsideBounds: 0
debug: debug:
m_Flags: 0 m_Flags: 0
m_SettingNames: m_SettingNames: