x


Move object after collision

Hello all,

I have 2 objects: the player and an enemy. The enemy is chasing the player and every time it hits the player, he will take damage. Everything is working as intended but I'm trying to "trow" the player backwards after the collision. How to do it? I can trow the player to any position, but I'm trying to figure how to throw him to the direction the enemy is facing, not where the player is facing. (This is needed because if the enemy hits the player, it usually hit 2-3 times before the player can move away from it).

Thanks for the help!!

more ▼

asked Mar 12 '11 at 06:35 PM

Finamore gravatar image

Finamore
14 3 4 6

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

1 answer: sort voted first

You can use enemy.forward to get the forward direction of the enemy. Of course you need to create the enemy variable (passed to you in your collision code).

function OnTriggerEnter(other : Collider)
{
    if (other.CompareTag("Enemy"))
    {
        var enemy = other.transform;
        ThrowDirection(enemy.forward);
    }
}

This assumes you have a function called ThrowDirection. I am sure you can work out how to adapt it to your script.

more ▼

answered Mar 12 '11 at 06:41 PM

Statement gravatar image

Statement ♦♦
20.1k 35 70 175

Thanks a lot!! Worked perfectly!!! Just one more detail I'm stuck: this function is "teleporting" the player, there's anyway to smooth this movement? I tried with deltatime and Vector3.SmoothDamp but no luck so far.

Mar 12 '11 at 08:08 PM Finamore

I don't know how your character script works but maybe you can just give it some force? Either via the rigidbody if you're using such, or via some character controller. Let gravity take care of it falling down. Or if possible, use an animation. If you go with the animation, maybe you could try out http://www.unifycommunity.com/wiki/index.php?title=AnimationStepSync which I wrote a while ago, and let me know if it works any decently :)

Mar 12 '11 at 10:56 PM Statement ♦♦
(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:

x2507
x1374
x888

asked: Mar 12 '11 at 06:35 PM

Seen: 1478 times

Last Updated: Mar 12 '11 at 06:35 PM