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_Name:
m_EditorClassIdentifier:
target: {fileID: 0}
detectRange: 15
attackRange: 1.5
isProvoked: 0
_detectRange: 15
_attackRange: 5
_rotationSpeed: 0.9
_target: {fileID: 0}
_isProvoked: 0
--- !u!195 &3924901601269037083
NavMeshAgent:
m_ObjectHideFlags: 0

View File

@ -1,58 +1,61 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class EnemyAI : MonoBehaviour
{
[SerializeField] Transform target;
[SerializeField] float detectRange = 15f;
[SerializeField] float attackRange = 5f;
[SerializeField] private bool isProvoked = false;
[Header("Configuration")]
[SerializeField] private float _detectRange = 15f;
[SerializeField] private float _attackRange = 5f;
[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 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
{
get => isProvoked;
set => isProvoked = value;
}
private void Awake()
{
target = FindObjectOfType<PlayerHealth>().transform;
get => _isProvoked;
set => _isProvoked = value;
}
void Start()
private void Awake()
{
navMeshAgent = GetComponent<NavMeshAgent>();
_target = FindObjectOfType<PlayerHealth>().transform;
}
private void Start()
{
_navMeshAgent = GetComponent<NavMeshAgent>();
_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();
}
}
private void EngageTarget()
{
if (DistanceToTarget(target.position) <= attackRange)
if (DistanceToTarget(_target.position) <= _attackRange)
{
AttackTarget();
}
else if (isProvoked)
else if (_isProvoked)
{
FollowTarget();
}
@ -64,25 +67,25 @@ public class EnemyAI : MonoBehaviour
private void FollowTarget()
{
_animator.SetTrigger("Move");
_animator.SetBool("Attack", false);
navMeshAgent.SetDestination(target.position);
_animator.SetTrigger(MoveAnimation);
_animator.SetBool(AttackAnimation, false);
_navMeshAgent.SetDestination(_target.position);
}
private void AttackTarget()
{
_animator.SetBool("Attack", true);
_animator.SetBool(AttackAnimation, true);
//print("Die Human!");
}
private void Idle()
{
_animator.SetTrigger("Idle");
_animator.SetTrigger(IdleAnimation);
}
private void SetStopDistance(float stopDistance)
{
navMeshAgent.stoppingDistance = stopDistance;
_navMeshAgent.stoppingDistance = stopDistance;
}
private float DistanceToTarget(Vector3 targetPosition)
@ -93,12 +96,11 @@ public class EnemyAI : MonoBehaviour
void OnDrawGizmosSelected()
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(transform.position, detectRange);
if (navMeshAgent != null)
Gizmos.DrawWireSphere(transform.position, _detectRange);
if (_navMeshAgent != null)
{
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;
public class EnemyAttack : MonoBehaviour
{
[SerializeField] Transform target;
[SerializeField] float damage = 40f;
[Header("Config")]
[SerializeField] private Transform _target;
[SerializeField] private float _damage = 40f;
private void Awake()
{
target = FindObjectOfType<PlayerHealth>().transform;
_target = FindObjectOfType<PlayerHealth>().transform;
}
private void AttackHitEvent()
{
if (target == null) return;
if(target.GetComponent<IDamageable>() != null)
{
Debug.Log($"{transform.name} Hits {target.transform.name}");
target.GetComponent<IDamageable>().ModifyHealth(-damage);
}
if (_target == null) return;
if (_target.GetComponent<IDamageable>() != null) return;
Debug.Log($"{transform.name} Hits {_target.transform.name}");
_target.GetComponent<IDamageable>().ModifyHealth(-_damage);
}
}

View File

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

View File

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

View File

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

View File

@ -1,21 +1,17 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class Weapon : MonoBehaviour
{
[SerializeField] private Camera _FpCamera;
[SerializeField] private ParticleSystem _MuzzleFlash;
[SerializeField] private GameObject _BulletImpact;
[SerializeField] private Camera _fpCamera;
[SerializeField] private ParticleSystem _muzzleFlash;
[SerializeField] private GameObject _bulletImpact;
[SerializeField] private float _range = 100f;
[SerializeField] private float _weaponDamage = 25f;
private void Awake()
{
_FpCamera = FindObjectOfType<Camera>();
_MuzzleFlash = GetComponentInChildren<ParticleSystem>();
_fpCamera = FindObjectOfType<Camera>();
_muzzleFlash = GetComponentInChildren<ParticleSystem>();
}
public void OnFire()
@ -32,17 +28,16 @@ public class Weapon : MonoBehaviour
{
Animation();
ProcessHit();
}
private void Animation()
{
_MuzzleFlash.Play();
_muzzleFlash.Play();
}
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!");
ImpactAnimation(hit);
@ -57,7 +52,7 @@ public class Weapon : MonoBehaviour
hit.transform.GetComponent<IDamageable>().ModifyHealth(-_weaponDamage);
}
if(hit.transform.GetComponent<EnemyAI>()!= null)
if (hit.transform.GetComponent<EnemyAI>() != null)
{
hit.transform.GetComponent<EnemyAI>().IsProvoked = true;
}
@ -65,7 +60,7 @@ public class Weapon : MonoBehaviour
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);
}
}