x


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?

more ▼

asked Nov 02 '10 at 01:24 AM

SirVictory gravatar image

SirVictory
1.7k 64 77 104

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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?

more ▼

answered Nov 02 '10 at 03:13 AM

Atnas1010 gravatar image

Atnas1010
1.1k 6 10 26

I'm such a novice, do I called this inside the ApplyDamage function? and if so, what what do i put in the param?

Nov 02 '10 at 03:41 AM SirVictory

like SlamBack(param);?

Nov 02 '10 at 03:42 AM SirVictory

Oh i think i got it! SlamBack(transform.TransformDirection(slamDirection)); it that the best way for me to do it?

Nov 02 '10 at 03:47 AM SirVictory

I'm not sure what that would do.. :) I would use SlamBack(slamDirection * strength)

Nov 02 '10 at 03:50 AM Atnas1010

SlamBack(slamDirection * -strength); worked for me, the other way around did the opposite.

Nov 02 '10 at 04:03 AM SirVictory
(comments are locked)
10|3000 characters needed characters left

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
more ▼

answered Nov 02 '10 at 02:34 AM

IJM gravatar image

IJM
1.4k 2 5 19

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.

Nov 02 '10 at 03:05 AM SirVictory

I meade a small edit on my Answer, I think that this will make your animation look right.

Nov 02 '10 at 03:39 AM IJM

Thanks for the initial answers. I wish I could check yours up also!

Nov 02 '10 at 04:04 AM SirVictory

Npa mate ;) I'm glad that I helped. Now when I see the answer, I realize that I didn't understand the question correctly =)

Nov 02 '10 at 04:55 AM IJM
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1866
x651
x11
x2

asked: Nov 02 '10 at 01:24 AM

Seen: 2077 times

Last Updated: Nov 02 '10 at 01:33 AM