- Home /
Staged bullet damage?
When my player shoots at enemies I want them to cause more damage the closer to the enemy's center they hit, and less damage if it's out to the sides. Right now though, my script simply checks if their bullets hit their character controller. How could I accomplish a different amount of damage depending on where it hits?
Answer by robertbu · Jun 01, 2013 at 02:19 AM
Here is something that might help you in a good direction. Note "center" can mean several things, pivot, center mass, center as the character is bisected. This code uses the pivot point. Because the pivot is (likely) inside the mesh, it will never score the maximum points.
#pragma strict
var maxDist = 1.0;
var minPoints = 10.0;
var maxPoints = 80.0;
function OnColliderEnter(col : Collision) {
if (col.collider.tag == "Bullet") {
var dist = (transform.position - col.contacts[0].point).magnitude;
var points = (maxDist - dist)/maxDist * maxPoints;
points = Mathf.Clamp(points, minPoints, maxPoints);
}
}
A good idea, thanks Robert. I am using character controllers on the enemies, so I'll need to port over to OnControllerColliderHit, but then I can use collision information and think I should be able to deter$$anonymous$$e how dead-on each shot hits. Thanks! :)
Your answer
Follow this Question
Related Questions
Enemy wont take Damage. How could i fix this?,Enemy wont take damage with raycast 1 Answer
AirStrike Scripting Help 0 Answers
coliders are not working 1 Answer
Rapid fire does damage every frame 0 Answers
Enemy shooting player driving me crazy! 2 Answers