- Home /
Punching - what would be the better way? (in javascript)
This is my first post. I made sure I search the related answers and the forums. I am trying to figure how to add damage to a rigidbody when it collides with my charactercontroller, so for my instance it would be punching. Before I go an corrupt my game by trying each way, I thought I would save time by asking for a second opinion.
One way I was told I could do it is by using AddForceExplosion on my character. But I was thinking that maybe I should make the radius of the capsule to my character a little wider and simply detect a collision.
What are your opinions on this?
Answer by AngryOldMan · Feb 28, 2011 at 07:34 PM
on the rigidbody implement a life system by making a variable that get's life subtracted from it every time you punch it. then when life = 0 destroy game object. want anymore info on how just say.
Thanks for the answer Bobadebob. I have already made a health system. What I am asking is how to get my character to punch an object and for it to be recognized the object has been hit. So I have my character and 1 unit gap between the character is an object i wish to hit. I am asking which option would be best in order to get a punching action working.
Ah i see, I think the method I would try (there may be a better way but Im sure that this would work) Would be to have a variable called something like "IsPunching" on your player which switches on automatically when your press punch and off when the punch animation is done.Then on the gameobject put a piece of script similar to this var PunchDamage : float = 10; var BoxLife : float = 20; Punched = PlayerPrefab.GetComponent(PlayerScript); if (Punched.Ispunching = true) { BoxLife = BoxLife - PunchDamage; }
Answer by Joel 5 · Mar 01, 2011 at 01:41 PM
So I went for the simple option of just detecting the collision in the capsule of the character controller. If anyone comes across this question looking for how to do the task above. Set your character controller capsule width to the width slightly shorter of your punch animation and just run a OnControllerColliderHit function to detect the hit. Pretty simple, will post if I come along any major problems with this technique.