Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

Damagehandler enemy

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using UnityEngine.AI;



public class DamageHandler_Enemy : vp_DamageHandler

{

    private Animator _animator;



    private NavMeshAgent _navMeshAgent;



    public GameObject Player;



    public float AttackDistance = 10.0f;



    protected override void Awake()

    {

        base.Awake();



        _navMeshAgent = GetComponent<NavMeshAgent>();



        _animator = GetComponent<Animator>();



    }



    public void Update()

    {

        if (_navMeshAgent.enabled)

        {

            float dist = Vector3.Distance(Player.transform.position, this.transform.position);

            if (dist < AttackDistance)

            {

                _navMeshAgent.SetDestination(Player.transform.position);

                _animator.SetBool("Attack", false);

                _animator.SetBool("IsFollow", true);

            }

            else

            {

                _animator.SetBool("Attack", false);

                _animator.SetBool("IsFollow", false);

                _navMeshAgent.SetDestination(transform.position);

            }



            // TODO: Get rid of this magic number (perhaps add property)

            if (dist < 3.0f)

            {

                _animator.SetBool("Attack", true);

                _animator.SetBool("IsFollow", false);

            }

        }

        

    }



    /// <summary>

    /// Character takes damage

    /// </summary>

    /// <param name="damageInfo"></param>

    public override void Damage(vp_DamageInfo damageInfo)

    {

        if (CurrentHealth > 0 )

        {



            AnimatorStateInfo si = _animator.GetCurrentAnimatorStateInfo(0);

            if (!si.IsName("Attack"))

            {



                base.Damage(damageInfo);



                _animator.Play("Hit", 0, 0.25f);

            }

        }

    }



    /// <summary>

    /// Character dies

    /// </summary>

    public override void Die()

    {

        if (!enabled || !vp_Utility.IsActive(gameObject))

            return;



        if (m_Audio != null)

        {

            m_Audio.pitch = Time.timeScale;

            m_Audio.PlayOneShot(DeathSound);

        }



        _navMeshAgent.enabled = false;



        _animator.SetBool("IsFollow", false);

        _animator.SetBool("Attack", false);



        _animator.SetTrigger("Dying");



        Destroy(GetComponent<vp_SurfaceIdentifier>());



    }



    /// <summary>

    /// Notice: Add the EndAttack event to the attack animation of your character

    /// </summary>

    public void EndAttack()

    {

        float dist = Vector3.Distance(Player.transform.position, this.transform.position);



        // TODO: Get rid of this magic number here: (perhaps add property)

        if(dist < 3.0f)

        {

            Player.SendMessage("Damage", 4.0f, SendMessageOptions.DontRequireReceiver);

        }

    }

}
Comment

PREVIOUS NEXT
Code Example
Csharp :: convert relative path to physical path c# 
Csharp :: switch rows and columns in a datagridview 
Csharp :: gegenstände bewegen c# 
Csharp :: save current dir shell 
Csharp :: rename join table in many to many 
Csharp :: c# get the return value of a func 
Csharp :: wpf clock conrt 
Csharp :: csharp test for null 
Csharp :: c# class where T : enum 
Csharp :: unity record animation at runtime 
Csharp :: c# read file while writing 
Csharp :: CS0234 compile error c# unity fix scene managment 
Csharp :: closedxm Iworksheet excel formulal 
Csharp :: how to clone something as a parent unity 
Csharp :: c# read single key 
Csharp :: Modify middleware response c# .net 
Csharp :: activeNetworkInfo depricated 
Csharp :: nest elasticsearch date reange c# .net 
Csharp :: how to export xml in linq c# 
Csharp :: Devexpress MVC Gridview BinaryImage Picture 
Csharp :: Computing a Cartesian product or Combinations with LINQ 
Csharp :: can you use unity for ftee 
Csharp :: c# get Full Exception message if InnerException is not NULL 
Csharp :: unity check if transform doent have parent 
Csharp :: single number c# 
Csharp :: c# how to debig 
Csharp :: c# lambda get all records async 
Csharp :: query to linq converter online 
Csharp :: cassandra Keyspaces repository .net 
Csharp :: hacker 
ADD CONTENT
Topic
Content
Source link
Name
4+4 =