- Home /
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!!
Answer by Statement · Mar 12, 2011 at 06:41 PM
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.
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.
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 :)
Your answer
