-
Notifications
You must be signed in to change notification settings - Fork 3
/
KnockbackAction.cs
105 lines (80 loc) · 3.09 KB
/
KnockbackAction.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// 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;
}
}
}
}