unity-abgabe2/Assets/Bots/Bot.cs

75 lines
2.0 KiB
C#
Raw Normal View History

2021-04-25 14:20:23 +00:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class Bot : MonoBehaviour
{
public GameObject target;
public LayerMask playerLayer;
public float seeRadius = 20f;
public float attkRadus = 2f;
private NavMeshAgent agent;
private Animator animator;
private float attackCooldown = 0f;
private int hp = 100;
void Start()
{
this.agent = this.GetComponent<NavMeshAgent>();
this.animator = this.GetComponentInChildren<Animator>();
}
void Update()
{
if (canSeePlayer())
{
if (canAttackPlayer()){
attack();
agent.isStopped = true;
}else{
agent.isStopped = false;
agent.SetDestination(target.transform.position);
}
}
UpdateAnimator(agent.desiredVelocity);
}
private void attack(){
attackCooldown -= Time.deltaTime;
if (attackCooldown <= 0f){
attackCooldown = 3f;
this.target.GetComponent<Controls>().hp -= 10;
this.target.GetComponent<Controls>().Damaged();
}
}
private bool canSeePlayer()
{
return Physics.CheckSphere(this.transform.position, seeRadius, playerLayer);
}
private bool canAttackPlayer(){
return Physics.CheckSphere(this.transform.position, attkRadus, playerLayer);
}
private void UpdateAnimator(Vector3 movement)
{
animator.SetFloat("Forward", movement.magnitude, 0.1f, Time.deltaTime);
if(movement.magnitude >= 1){
Debug.Log(Mathf.Atan2(movement.x, movement.z));
}
animator.SetFloat("Turn", Mathf.Atan2(movement.x, movement.z), 0.1f, Time.deltaTime);
}
public void hit(){
this.hp -= 20;
agent.isStopped = false;
agent.SetDestination(target.transform.position);
if (this.hp <= 0){
Destroy(this.gameObject);
}
}
}