This commit is contained in:
Stedd 2022-10-14 18:25:19 +02:00
parent c18a4ace11
commit 74a95468a5
7 changed files with 76 additions and 94 deletions

View File

@ -63,10 +63,11 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 9288de7ba2b80ce43be15a0ff0aa307a, type: 3} m_Script: {fileID: 11500000, guid: 9288de7ba2b80ce43be15a0ff0aa307a, type: 3}
m_Name: m_Name:
m_EditorClassIdentifier: m_EditorClassIdentifier:
target: {fileID: 0} _detectRange: 15
detectRange: 15 _attackRange: 5
attackRange: 1.5 _rotationSpeed: 0.9
isProvoked: 0 _target: {fileID: 0}
_isProvoked: 0
--- !u!195 &3924901601269037083 --- !u!195 &3924901601269037083
NavMeshAgent: NavMeshAgent:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0

View File

@ -1,58 +1,61 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine; using UnityEngine;
using UnityEngine.AI; using UnityEngine.AI;
public class EnemyAI : MonoBehaviour public class EnemyAI : MonoBehaviour
{ {
[SerializeField] Transform target; [Header("Configuration")]
[SerializeField] float detectRange = 15f; [SerializeField] private float _detectRange = 15f;
[SerializeField] float attackRange = 5f; [SerializeField] private float _attackRange = 5f;
[SerializeField] private bool isProvoked = false; [SerializeField] private float _rotationSpeed = 0.1f;
[Header("State")]
[SerializeField] private Transform _target;
[SerializeField] private bool _isProvoked;
private NavMeshAgent navMeshAgent; private NavMeshAgent _navMeshAgent;
private Animator _animator; private Animator _animator;
private static readonly int MoveAnimation = Animator.StringToHash("Move");
private static readonly int AttackAnimation = Animator.StringToHash("Attack");
private static readonly int IdleAnimation = Animator.StringToHash("Idle");
public bool IsProvoked public bool IsProvoked
{ {
get => isProvoked; get => _isProvoked;
set => isProvoked = value; set => _isProvoked = value;
}
private void Awake()
{
target = FindObjectOfType<PlayerHealth>().transform;
} }
void Start() private void Awake()
{ {
navMeshAgent = GetComponent<NavMeshAgent>(); _target = FindObjectOfType<PlayerHealth>().transform;
}
private void Start()
{
_navMeshAgent = GetComponent<NavMeshAgent>();
_animator = GetComponent<Animator>(); _animator = GetComponent<Animator>();
} }
void Update() private void Update()
{ {
SetStopDistance(attackRange * 0.9f); SetStopDistance(_attackRange * 0.9f);
if (DistanceToTarget(target.position) < detectRange) if (DistanceToTarget(_target.position) < _detectRange)
{ {
isProvoked = true; _isProvoked = true;
} }
if (isProvoked) if (_isProvoked)
{ {
EngageTarget(); EngageTarget();
} }
} }
private void EngageTarget() private void EngageTarget()
{ {
if (DistanceToTarget(target.position) <= attackRange) if (DistanceToTarget(_target.position) <= _attackRange)
{ {
AttackTarget(); AttackTarget();
} }
else if (isProvoked) else if (_isProvoked)
{ {
FollowTarget(); FollowTarget();
} }
@ -64,25 +67,25 @@ public class EnemyAI : MonoBehaviour
private void FollowTarget() private void FollowTarget()
{ {
_animator.SetTrigger("Move"); _animator.SetTrigger(MoveAnimation);
_animator.SetBool("Attack", false); _animator.SetBool(AttackAnimation, false);
navMeshAgent.SetDestination(target.position); _navMeshAgent.SetDestination(_target.position);
} }
private void AttackTarget() private void AttackTarget()
{ {
_animator.SetBool("Attack", true); _animator.SetBool(AttackAnimation, true);
//print("Die Human!"); //print("Die Human!");
} }
private void Idle() private void Idle()
{ {
_animator.SetTrigger("Idle"); _animator.SetTrigger(IdleAnimation);
} }
private void SetStopDistance(float stopDistance) private void SetStopDistance(float stopDistance)
{ {
navMeshAgent.stoppingDistance = stopDistance; _navMeshAgent.stoppingDistance = stopDistance;
} }
private float DistanceToTarget(Vector3 targetPosition) private float DistanceToTarget(Vector3 targetPosition)
@ -93,12 +96,11 @@ public class EnemyAI : MonoBehaviour
void OnDrawGizmosSelected() void OnDrawGizmosSelected()
{ {
Gizmos.color = Color.red; Gizmos.color = Color.red;
Gizmos.DrawWireSphere(transform.position, detectRange); Gizmos.DrawWireSphere(transform.position, _detectRange);
if (navMeshAgent != null) if (_navMeshAgent != null)
{ {
Gizmos.color = Color.blue; Gizmos.color = Color.blue;
Gizmos.DrawWireSphere(transform.position, navMeshAgent.stoppingDistance); Gizmos.DrawWireSphere(transform.position, _navMeshAgent.stoppingDistance);
} }
} }
} }

View File

@ -1,25 +1,21 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine; using UnityEngine;
public class EnemyAttack : MonoBehaviour public class EnemyAttack : MonoBehaviour
{ {
[SerializeField] Transform target; [Header("Config")]
[SerializeField] float damage = 40f; [SerializeField] private Transform _target;
[SerializeField] private float _damage = 40f;
private void Awake() private void Awake()
{ {
target = FindObjectOfType<PlayerHealth>().transform; _target = FindObjectOfType<PlayerHealth>().transform;
} }
private void AttackHitEvent() private void AttackHitEvent()
{ {
if (target == null) return; if (_target == null) return;
if(target.GetComponent<IDamageable>() != null) if (_target.GetComponent<IDamageable>() != null) return;
{ Debug.Log($"{transform.name} Hits {_target.transform.name}");
Debug.Log($"{transform.name} Hits {target.transform.name}"); _target.GetComponent<IDamageable>().ModifyHealth(-_damage);
target.GetComponent<IDamageable>().ModifyHealth(-damage);
}
} }
}

View File

@ -1,12 +1,11 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine; using UnityEngine;
public class EnemyHealth : MonoBehaviour, IDamageable public class EnemyHealth : MonoBehaviour, IDamageable
{ {
[SerializeField] float _maxHealth = 100f; [Header("Config")]
[SerializeField] float _health ; [SerializeField] private float _maxHealth = 100f;
[Header("State")]
[SerializeField] private float _health;
private void OnEnable() private void OnEnable()
{ {
@ -15,16 +14,14 @@ public class EnemyHealth : MonoBehaviour, IDamageable
private void Update() private void Update()
{ {
if (_health <= 0) if (!(_health <= 0)) return;
{ print("Ded");
print("Ded"); Destroy(gameObject);
Destroy(gameObject);
}
} }
public void ModifyHealth(float _healthChange) public void ModifyHealth(float healthChange)
{ {
_health += _healthChange; _health += healthChange;
} }
public void SetHealth(float newHealth) public void SetHealth(float newHealth)
@ -51,5 +48,4 @@ public class EnemyHealth : MonoBehaviour, IDamageable
{ {
return _health / _maxHealth; return _health / _maxHealth;
} }
} }

View File

@ -1,10 +1,6 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
interface IDamageable interface IDamageable
{ {
public void ModifyHealth(float _healthChange); public void ModifyHealth(float healthChange);
public void SetHealth(float newHealth); public void SetHealth(float newHealth);

View File

@ -1,11 +1,9 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine; using UnityEngine;
public class PlayerHealth : MonoBehaviour, IDamageable public class PlayerHealth : MonoBehaviour, IDamageable
{ {
[SerializeField] float _maxHealth; [SerializeField] private float _maxHealth;
[SerializeField] float _health; [SerializeField] private float _health;
private void Awake() private void Awake()
{ {
@ -35,9 +33,9 @@ public class PlayerHealth : MonoBehaviour, IDamageable
return _maxHealth; return _maxHealth;
} }
public void ModifyHealth(float _healthChange) public void ModifyHealth(float healthChange)
{ {
_health += _healthChange; _health += healthChange;
} }
public void SetHealth(float newHealth) public void SetHealth(float newHealth)
@ -49,6 +47,4 @@ public class PlayerHealth : MonoBehaviour, IDamageable
{ {
_maxHealth = newHealth; _maxHealth = newHealth;
} }
} }

View File

@ -1,21 +1,17 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine; using UnityEngine;
using UnityEngine.InputSystem;
public class Weapon : MonoBehaviour public class Weapon : MonoBehaviour
{ {
[SerializeField] private Camera _FpCamera; [SerializeField] private Camera _fpCamera;
[SerializeField] private ParticleSystem _MuzzleFlash; [SerializeField] private ParticleSystem _muzzleFlash;
[SerializeField] private GameObject _BulletImpact; [SerializeField] private GameObject _bulletImpact;
[SerializeField] private float _range = 100f; [SerializeField] private float _range = 100f;
[SerializeField] private float _weaponDamage = 25f; [SerializeField] private float _weaponDamage = 25f;
private void Awake() private void Awake()
{ {
_FpCamera = FindObjectOfType<Camera>(); _fpCamera = FindObjectOfType<Camera>();
_MuzzleFlash = GetComponentInChildren<ParticleSystem>(); _muzzleFlash = GetComponentInChildren<ParticleSystem>();
} }
public void OnFire() public void OnFire()
@ -32,17 +28,16 @@ public class Weapon : MonoBehaviour
{ {
Animation(); Animation();
ProcessHit(); ProcessHit();
} }
private void Animation() private void Animation()
{ {
_MuzzleFlash.Play(); _muzzleFlash.Play();
} }
private void ProcessHit() private void ProcessHit()
{ {
if (Physics.Raycast(_FpCamera.transform.position, _FpCamera.transform.forward, out RaycastHit hit, _range)) if (Physics.Raycast(_fpCamera.transform.position, _fpCamera.transform.forward, out RaycastHit hit, _range))
{ {
//print($"{hit.transform.name} was hit!"); //print($"{hit.transform.name} was hit!");
ImpactAnimation(hit); ImpactAnimation(hit);
@ -57,7 +52,7 @@ public class Weapon : MonoBehaviour
hit.transform.GetComponent<IDamageable>().ModifyHealth(-_weaponDamage); hit.transform.GetComponent<IDamageable>().ModifyHealth(-_weaponDamage);
} }
if(hit.transform.GetComponent<EnemyAI>()!= null) if (hit.transform.GetComponent<EnemyAI>() != null)
{ {
hit.transform.GetComponent<EnemyAI>().IsProvoked = true; hit.transform.GetComponent<EnemyAI>().IsProvoked = true;
} }
@ -65,7 +60,7 @@ public class Weapon : MonoBehaviour
private void ImpactAnimation(RaycastHit hit) private void ImpactAnimation(RaycastHit hit)
{ {
GameObject impactEffect = Instantiate(_BulletImpact, hit.point, Quaternion.LookRotation(hit.normal)); GameObject impactEffect = Instantiate(_bulletImpact, hit.point, Quaternion.LookRotation(hit.normal));
Destroy(impactEffect, 1); Destroy(impactEffect, 1);
} }
} }