- Home /
How can my character stop moving immediately after it gets hit by enemy?
My character have a rigidbody and using a third person controller to make it move. I have add some script to let the character cannot move and attack when it get hit by enemy.
After that, when it get hitted it seems have an inertia so that it is playing my "hit" animation and sliding towards a direction which I get hit before. How can I solved this sliding problem?
I have try to put the below script when the player is play hit and attack animation.
rigidbody.velocity = Vector3.zero;
But it is not work and still sliding away from the last position.
Answer by Herman-Tulleken · May 20, 2011 at 07:28 AM
Here is how we freeze (and unfreeze) our physics for objects. (We use it for Pause/Play but you should be able to adapt it for your case):
public void Pause()
{
velocity = rigidbody.velocity;
angularVelocity = rigidbody.angularVelocity;
rigidbody.velocity = Vector3.zero;
rigidbody.angularVelocity = Vector3.zero;
rigidbody.useGravity = false;
rigidbody.isKinematic = true;
}
public void Play()
{
rigidbody.isKinematic = false;
rigidbody.velocity = velocity;
rigidbody.angularVelocity = angularVelocity;
rigidbody.useGravity = true;
}
it still cannot work. And it say actor must be (non-kinematic) dynamic What's wrong is my character?
Hey man, have you actually setup the logic for shooting and getting hit? Eg calling something like when the charracter is hit:
function hit(){
velocity = rigidbody.velocity; angularVelocity = rigidbody.angularVelocity; rigidbody.velocity = Vector3.zero; rigidbody.angularVelocity = Vector3.zero; rigidbody.useGravity = false; }
Dude for real? Rigidbody.is$$anonymous$$inematic....
And please don't post comments as answers. Converting for you.
Your answer
Follow this Question
Related Questions
Modifying Gravity for Local Player 1 Answer
Mecanim Inherit Velocity of previous animation? 0 Answers
How to calculate the speed at which a rigidbody2d is actually moving ? 1 Answer
Change velocity of Character Controller 2 Answers
Velocity powered rigidbody on a moving platform without parenting. 3 Answers