- Home /
Hitting an Enemy and Pushing them back
I want to knock back an enemy when they are hit by the player. The enemy has a custom script that handles gravity (It has a character controller).
Here is the script:
//addGravity private var walkSpeed : float = 1.0; var gravity =20.0;
// The current move direction in x-z var moveDirection : Vector3 = Vector3.zero; // The current vertical speed var verticalSpeed = 0.0; // The current x-z move speed var moveSpeed = 0.0;
private var charController : CharacterController;
function Start() { charController = GetComponent(CharacterController); animation.wrapMode = WrapMode.Loop; }
function ApplyGravity() { verticalSpeed -= gravity * Time.deltaTime; }
function Update () { ApplyGravity(); var movement = moveDirection moveSpeed + Vector3 (0, verticalSpeed, 0) ;//+ inAirVelocity; charController.Move(movement (Time.deltaTime));
if (charController.isGrounded) {verticalSpeed = 0;} }
Now, I have a script that handles the damage calculation when they are hit. I want to handle the knockback here. Here is the function from that script:
function ApplyDamage (damage : int) { var myself : addGravity = GetComponent(addGravity); charController = GetComponent(CharacterController);
flinch= true;
eA.SendMessage ("Flinch"); //Sending this to the Flinch function in the AIFollow.cs Script
animation.Play("gotbit");
// We've been hit, so play the 'struck' sound. This should be a metallic 'clang'.
if (audio && struckSound)
audio.PlayOneShot(struckSound);
if (hitPoints <= 0)
return;
//Knockback Physics
//======================================================================//
var pos = transform.TransformPoint(punchPosition);
var slamDirection = transform.InverseTransformDirection(target.position - transform.position);
charController.Move(slamDirection * strength);
myself.verticalSpeed = 5;
hitPoints -= damage;
//======================================================================//
if (!dead && hitPoints <= 0)
{
Die();
dead = true;
}
//This will set the flinch to false
yield WaitForSeconds (flinchTime);
flinch = false;
eA.SendMessage ("unFlinch"); //Sending this to the unflinch function in the AIFollow.cs Script
}
This line is suppose to move the enemy in the opposite direction (even though right now, its more like warping and I'm not sure how to fix that, maybe time.delta?) but it only works for one direction.
charController.Move(slamDirection * strength);
Can anyone help me with this?
Answer by Atnas1010 · Nov 02, 2010 at 03:13 AM
I would place this function on the enemy, and call SlamBack when he has to get slammed.
function SlamBack(slamDistance : Vector3)
{
var slamTime = Time.time;
while (slamTime+smoothTime > Time.time)
{
charController.Move(-slamDistance*(slamTime+smoothTime-Time.time)/smoothTime*Time.deltaTime);
yield;
}
}
To get him slammed longer, you make slamDistance greater, and to make him come to a rest slower, you edit smoothTime.
Is this the kind of interpolation you are looking for?
I'm such a novice, do I called this inside the ApplyDamage function? and if so, what what do i put in the param?
Oh i think i got it! SlamBack(transform.TransformDirection(slamDirection)); it that the best way for me to do it?
I'm not sure what that would do.. :) I would use SlamBack(slamDirection * strength)
SlamBack(slamDirection * -strength); worked for me, the other way around did the opposite.
Answer by IJM · Nov 02, 2010 at 02:34 AM
I am sure of one thing:
slamDirection = PlayersGameObject.transform.forward;
I hope this has helped you =)
EDIT:
You can try this for the animation:
enemy.transform.LookAt(Player.transform.position);//call this once the enemy is hit
Thanks, this works as far as sending the enemy in the right directions, but how would I interpolate the motion? Right now the enemy just kind of warps ahead.
I meade a small edit on my Answer, I think that this will make your animation look right.
Thanks for the initial answers. I wish I could check yours up also!
Npa mate ;) I'm glad that I helped. Now when I see the answer, I realize that I didn't understand the question correctly =)
Your answer
Follow this Question
Related Questions
Character Controllers Knockback 1 Answer
Force on Character Controller (Knockback) 4 Answers
Need help with knockback in C# Please! 2 Answers
How to make arch knockback 3 Answers