PlayMaker Updates & Downloads > Share New Actions

Npc Knockback action

(1/2) > >>

mdotstrange:

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: ---// 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;

            }

           


         


        }


    }
}
--- End code ---

600:
Thanks! Will try it out later, I can see it used on car collisions too!

terri:
cool, thanks!

tcmeric:
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:
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

Navigation

[0] Message Index

[#] Next page

Go to full version