playMaker

Author Topic: Npc Knockback action  (Read 3718 times)

mdotstrange

  • Hero Member
  • *****
  • Posts: 555
    • View Profile
    • Can't code? Who cares! Make games anyway!
Npc Knockback action
« on: January 14, 2017, 10:17:21 PM »

This action is meant to be used on an Npc to knock it back when its damaged/hit- it works with Npc's that use NavmeshAgent, RigidBody, or CharacterControllers for movement. Just choose the one you want in the popup. It will knock the Npc back in the direction of what it was hit by.

On your projectile or melee weapon- use a cast(raycast, spherecast, etc)- get the hitpoint from the cast and pass it to this action through the "HitPoint" var and the action will do the rest.

Code: [Select]
// by MDS
using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
    [ActionCategory(ActionCategory.Physics)]
    [Tooltip("Applies knock to a game object using a Rigidbody, NavmeshAgent or Character Controller")]
    public class KnockbackAction : FsmStateAction
    {
        [RequiredField]
        [Tooltip("The object to knockback")]
        public FsmOwnerDefault knockThisBack;
        [Tooltip("Select type- selected component must be present on object or it won't work")]
        public enum objectType {NavMeshAgent,RigidBody,CharacterController }
        public objectType type;
        private Vector3 npcPos;
        [Tooltip("The hitPoint from a cast")]
        public FsmVector3 hitPoint;
        [Tooltip("How far to knock the object back")]
        public FsmFloat knockBackAmount;
        Rigidbody rb;
        NavMeshAgent nav;
        CharacterController cc;

        public bool everyFrame;

        public override void Reset()
        {
            everyFrame = false;
        }

        public override void OnEnter()
        {


            DoKnockBackAction();

            if (!everyFrame)
            {
                Finish();
            }
        }

        public override void OnUpdate()
        {
            DoKnockBackAction();
        }

        void DoKnockBackAction()
        {
            var go = Fsm.GetOwnerDefaultTarget(knockThisBack);
            if (go == null)
            {
                return;
            }

            int eValue = (int)type;

            switch(eValue)
            {
                case 0:
                    // navmesh
                    nav = go.GetComponent<NavMeshAgent>();
                    npcPos = go.transform.position;
                    Vector3 direction1 = (npcPos - hitPoint.Value).normalized;
                    direction1 = direction1 * knockBackAmount.Value / 2;
                    direction1 = new Vector3(direction1.x, 0f, direction1.z);
                    nav.Move(direction1);
                   

                    break;
                case 1:
                    // rb
                    rb = go.GetComponent<Rigidbody>();
                    npcPos = go.transform.position;
                    Vector3 direction = (npcPos - hitPoint.Value).normalized;
                    direction = direction * knockBackAmount.Value;
                    direction = new Vector3(direction.x, 0f, direction.z);
                    rb.AddForce(direction, ForceMode.VelocityChange);

                    break;
                case 2:
                    // cc
                    cc = go.GetComponent<CharacterController>();
                    npcPos = go.transform.position;
                    Vector3 direction2 = (npcPos - hitPoint.Value).normalized;
                    direction2 = direction2 * knockBackAmount.Value * 10f;
                    direction2 = new Vector3(direction2.x, 0f, direction2.z);
                    cc.Move(direction2 * Time.deltaTime);

                    break;

            }

           


         


        }


    }
}

Indie game dev and instructor at the Strange School. Learn Playmaker at the Strange School!

600

  • Beta Group
  • Hero Member
  • *
  • Posts: 641
    • View Profile
    • Flashing Lights
Re: Npc Knockback action
« Reply #1 on: January 15, 2017, 06:34:17 AM »
Thanks! Will try it out later, I can see it used on car collisions too!

terri

  • Sr. Member
  • ****
  • Posts: 375
    • View Profile
    • terrivellmann.tumblr.com
Re: Npc Knockback action
« Reply #2 on: January 15, 2017, 11:03:27 AM »
cool, thanks!

tcmeric

  • Beta Group
  • Hero Member
  • *
  • Posts: 768
    • View Profile
Re: Npc Knockback action
« Reply #3 on: March 29, 2017, 06:32:18 AM »
I hope you don't mind I made an adjustment on the character controller type. In the original, the character controller just moves to the new position (instantly), which can be a bit jarring.

I added an itween (and removed by the power of 10). It works for me, but its open to review if you see a mistake. Ive tested it and it works good for me. With the CC, i just set the knockback to 1 and the itween to 1. Seems about right.

I changed the action name a bit, just so I didnt write over yours: https://github.com/dumbgamedev/general-playmaker/blob/master/Math/KnockbackActionItween.cs

ynys

  • Playmaker Newbie
  • *
  • Posts: 1
    • View Profile
Re: Npc Knockback action
« Reply #4 on: May 20, 2018, 02:16:24 PM »
hi thanks for making this action!
i tried using the knockbackactionitween with a trigger event and get collision info storing the contact point as a varible that is the hit point.
But it seems to knock the player back and then the next frame back to the position it was in. i've probably set this up wrong as its meant to used with a raycast.
let me know if you can think of an easy solution or i should just do a raycast aswell to get the hit point.
thanks again!!

j

Qbanyto

  • Junior Playmaker
  • **
  • Posts: 66
  • Vista Larga, Paso corto
    • View Profile
Re: Npc Knockback action
« Reply #5 on: November 24, 2020, 11:01:25 AM »
I know this is from 2018 but the action itself still works as I just tested it. the only issue i am having is that the enemy gets knocked back to the same diagonal location no matter which way he is facing. I need to to get knocked back in the opposite directions that he is facing. So If i hit him from behind, he gets knocked back TO ME...which doesn't make sense lol


Qbanyto

  • Junior Playmaker
  • **
  • Posts: 66
  • Vista Larga, Paso corto
    • View Profile
Re: Npc Knockback action
« Reply #6 on: November 24, 2020, 11:13:36 AM »
Ok I figured it out. So in the "hit point" you have to put a variable. in my case is my hero's position which i have as a global variable. and now the enemy knocks back depending where the hero's or source of attacks is coming from.