playMaker

Author Topic: Npc Knockback action  (Read 2197 times)

mdotstrange

  • Hero Member
  • *****
  • Posts: 549
    • 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: 579
    • View Profile
    • Car and Traffic Playmaker assets
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: 373
    • View Profile
    • terrivellmann.tumblr.com
Re: Npc Knockback action
« Reply #2 on: January 15, 2017, 11:03:27 AM »
cool, thanks!

tcmeric

  • Hero Member
  • *****
  • Posts: 666
    • 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