Fix physics with navmesh agent
- desactivate navmesh temporarly when knockback (MovementHandler TakeImpulse) - Delete unit when HealthHandler Death called - Made 150 units fight (for performance test)
This commit is contained in:
parent
9e582b1a79
commit
0c0b9287ce
8 changed files with 9344 additions and 121 deletions
15
Assets/Materials/UnitPhysics.physicMaterial
Normal file
15
Assets/Materials/UnitPhysics.physicMaterial
Normal file
|
@ -0,0 +1,15 @@
|
|||
%YAML 1.1
|
||||
%TAG !u! tag:unity3d.com,2011:
|
||||
--- !u!134 &13400000
|
||||
PhysicsMaterial:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_Name: UnitPhysics
|
||||
serializedVersion: 2
|
||||
m_DynamicFriction: 0.6
|
||||
m_StaticFriction: 0.6
|
||||
m_Bounciness: 0
|
||||
m_FrictionCombine: 0
|
||||
m_BounceCombine: 0
|
8
Assets/Materials/UnitPhysics.physicMaterial.meta
Normal file
8
Assets/Materials/UnitPhysics.physicMaterial.meta
Normal file
|
@ -0,0 +1,8 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 6d59c543e7995191cbee8fab25167b46
|
||||
NativeFormatImporter:
|
||||
externalObjects: {}
|
||||
mainObjectFileID: 13400000
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -18,7 +18,7 @@ GameObject:
|
|||
- component: {fileID: 8213395333053285225}
|
||||
- component: {fileID: 8908285060348773052}
|
||||
- component: {fileID: 6587310683195830629}
|
||||
m_Layer: 0
|
||||
m_Layer: 6
|
||||
m_Name: TestUnit
|
||||
m_TagString: Unit
|
||||
m_Icon: {fileID: 0}
|
||||
|
@ -120,8 +120,8 @@ Rigidbody:
|
|||
m_UseGravity: 1
|
||||
m_IsKinematic: 0
|
||||
m_Interpolate: 0
|
||||
m_Constraints: 0
|
||||
m_CollisionDetection: 0
|
||||
m_Constraints: 80
|
||||
m_CollisionDetection: 1
|
||||
--- !u!195 &5811210244409818000
|
||||
NavMeshAgent:
|
||||
m_ObjectHideFlags: 0
|
||||
|
@ -151,7 +151,7 @@ CapsuleCollider:
|
|||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 2076979688870881298}
|
||||
m_Material: {fileID: 0}
|
||||
m_Material: {fileID: 13400000, guid: 6d59c543e7995191cbee8fab25167b46, type: 2}
|
||||
m_IncludeLayers:
|
||||
serializedVersion: 2
|
||||
m_Bits: 0
|
||||
|
@ -212,10 +212,11 @@ MonoBehaviour:
|
|||
m_Script: {fileID: 11500000, guid: eb03fb8c097f016f09d345ce200c3f41, type: 3}
|
||||
m_Name:
|
||||
m_EditorClassIdentifier:
|
||||
damage: 10
|
||||
damage: 20
|
||||
cooldown: 2
|
||||
attackShape: {fileID: 2074347425566192522}
|
||||
knockback: 2
|
||||
knockbackHorizontalForce: 8
|
||||
knockbackVerticalForce: 5
|
||||
--- !u!114 &6587310683195830629
|
||||
MonoBehaviour:
|
||||
m_ObjectHideFlags: 0
|
||||
|
@ -361,7 +362,7 @@ BoxCollider:
|
|||
m_Material: {fileID: 0}
|
||||
m_IncludeLayers:
|
||||
serializedVersion: 2
|
||||
m_Bits: 0
|
||||
m_Bits: 64
|
||||
m_ExcludeLayers:
|
||||
serializedVersion: 2
|
||||
m_Bits: 0
|
||||
|
|
File diff suppressed because it is too large
Load diff
Binary file not shown.
|
@ -1,6 +1,8 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Serialization;
|
||||
using Random = UnityEngine.Random;
|
||||
|
||||
[RequireComponent(typeof(Unit))]
|
||||
public class AttackHandler : MonoBehaviour
|
||||
|
@ -8,7 +10,8 @@ public class AttackHandler : MonoBehaviour
|
|||
[SerializeField] private float damage;
|
||||
[SerializeField] private float cooldown;
|
||||
[SerializeField] private Collider attackShape;
|
||||
[SerializeField] private float knockback;
|
||||
[SerializeField] private float knockbackHorizontalForce;
|
||||
[SerializeField] private float knockbackVerticalForce;
|
||||
|
||||
private float _timer;
|
||||
private Unit _unit;
|
||||
|
@ -20,7 +23,8 @@ public class AttackHandler : MonoBehaviour
|
|||
|
||||
void Start()
|
||||
{
|
||||
_timer = cooldown;
|
||||
// Random to avoid too much synchronicity
|
||||
_timer = cooldown + Random.Range(-cooldown*0.2f, cooldown*0.2f);
|
||||
}
|
||||
|
||||
void Update()
|
||||
|
@ -38,37 +42,36 @@ public class AttackHandler : MonoBehaviour
|
|||
if (_timer > 0) return false;
|
||||
|
||||
Collider[] targets = DetectTargets();
|
||||
print(targets.Length);
|
||||
foreach (Collider target in targets)
|
||||
{
|
||||
if (!target.CompareTag("Unit")) continue;
|
||||
// GetComponent is expensive in performance, optimize here if it's slow
|
||||
Unit unit = target.GetComponent<Unit>();
|
||||
Unit targetUnit = target.GetComponent<Unit>();
|
||||
|
||||
// No friendly fire
|
||||
if (unit.IsTeamA == _unit.IsTeamA) continue;
|
||||
if (targetUnit.IsTeamA == _unit.IsTeamA) continue;
|
||||
|
||||
unit.Health.TakeDamage(damage);
|
||||
Vector3 knockbackVector = knockback * (target.transform.position - transform.position).normalized;
|
||||
unit.Body.AddForce(knockbackVector, ForceMode.Impulse);
|
||||
|
||||
targetUnit.Health.TakeDamage(damage);
|
||||
Vector3 knockbackVector = knockbackHorizontalForce * (target.transform.position - transform.position).normalized
|
||||
+ knockbackVerticalForce * Vector3.up;
|
||||
|
||||
targetUnit.StartCoroutine(targetUnit.Move.TakeImpulse(knockbackVector));
|
||||
}
|
||||
_timer = cooldown;
|
||||
_timer = cooldown + Random.Range(-cooldown*0.2f, cooldown*0.2f);
|
||||
return true;
|
||||
}
|
||||
|
||||
private Collider[] DetectTargets()
|
||||
{
|
||||
// Make sure to manager layers for better performance
|
||||
Collider[] hitColliders;
|
||||
|
||||
switch (attackShape)
|
||||
{
|
||||
case SphereCollider sphere:
|
||||
hitColliders = Physics.OverlapSphere(sphere.transform.position, sphere.radius);
|
||||
hitColliders = Physics.OverlapSphere(sphere.transform.position, sphere.radius, sphere.includeLayers);
|
||||
break;
|
||||
case BoxCollider box:
|
||||
hitColliders = Physics.OverlapBox(box.bounds.center, box.bounds.extents, box.transform.rotation);
|
||||
hitColliders = Physics.OverlapBox(box.bounds.center, box.bounds.extents, box.transform.rotation, box.includeLayers);
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentException("Only sphere or box are supported");
|
||||
|
|
|
@ -55,6 +55,8 @@ public class HealthHandler : MonoBehaviour
|
|||
{
|
||||
GlobalsVariable.AliveUnitsTeamB.Remove(_unit);
|
||||
}
|
||||
|
||||
Destroy(gameObject);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -12,8 +12,8 @@ TagManager:
|
|||
-
|
||||
- Water
|
||||
- UI
|
||||
-
|
||||
-
|
||||
- Units
|
||||
- NavmeshSurface
|
||||
-
|
||||
-
|
||||
-
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue