NPC.ExecuteAttack C# (CSharp) Method

ExecuteAttack() public method

NPC tries to hit the player.
public ExecuteAttack ( ) : void
return void
    public void ExecuteAttack()
    {
        if(gtarg==null)
        {
            return;
        }
        float weaponRange=1.5f;

        //NPC tries to raycast at the player or object

        Vector3 TargetingPoint;
        if (gtarg.name=="_Gronk")
        {//Try and hit the player
            TargetingPoint=GameWorldController.instance.playerUW.transform.position;
        }
        else
        {//Trying to hit an object
            TargetingPoint=gtarg.GetComponent<ObjectInteraction>().GetImpactPoint();//Aims for the objects impact point
        }

        Ray ray= new Ray(NPC_Launcher.transform.position,TargetingPoint-NPC_Launcher.transform.position);

        RaycastHit hit = new RaycastHit();
        if (Physics.Raycast(ray,out hit,weaponRange))
        {
            if (hit.transform.Equals(this.transform))
            {
                Debug.Log ("you've hit yourself ? " + hit.transform.name);
            }
            else
            {
            if (hit.transform.name == GameWorldController.instance.playerUW.name)
                {
                    MusicController.LastAttackCounter=30.0f; //Thirty more seconds of combat music
                    GameWorldController.instance.playerUW.ApplyDamage(5,this.gameObject);
                }
            else
                {
                    //Has it hit another npc or object
                    if (hit.transform.GetComponent<ObjectInteraction>()!=null)
                    {
                        hit.transform.GetComponent<ObjectInteraction>().Attack(5, this.gameObject);
                    }
                }
            }
        }
    }

Usage Example

Example #1
0
    public override ActionResult Execute(RAIN.Core.AI ai)
    {
        NPC npc = ai.Body.GetComponent <NPC>();

        npc.ExecuteAttack();

        return(ActionResult.SUCCESS);
    }