- Home /
How to add hitboxes firstperson shooting
Im wondering how I can go about differentiating different body parts of the player using colliders how would I go about checking individual colliders thru code (ex if a player gets shot in the head vs the body) how can I chech which collider is getting hit and change the damage value getting sent to the player health Sorry if this sounds confusing im having trouble wording it right now
And if I got this working would sphere colliders or box colliders be a better choice
what suits the shape of your bodies better. Just make each body part a transform (in case of a rig that's already the case) and add one collider each. when you detect these by raycast you could use the names. if you do this by real collision you'd need to check each point of the collision objects contacts and see what otherCollider was.
if these colliders do not handle anything besides hit detection, they could just be made triggers, for raycasting and triggering with other colliders.
Answer by Rexro · Mar 21, 2018 at 02:17 AM
Are all the body parts separate child objects of the player? If so I would create a script to add to all the body parts in which would have a OnCollisionEnter()
method. In this method I would call a method from a script attached to the player which would take in a parameter determining which body part just called it (I will use an enum) and use if statements to deal different damages. Here is the script attached to all the body parts:
public class ChildCollision : MonoBehaviour {
GameObject parent;
public BodyTypes thisPart;
void Start () {
parent = transform.parent.gameObject;
}
private void OnCollisionEnter(Collision collision)
{
parent.GetComponent<ParentCollision>().OnChildCollisionEnter(thisPart);
}
}
And Here is the script Attached to the player:
public enum BodyTypes
{
Head, Body, Arms, Legs
}
public class ParentCollision : MonoBehaviour {
public void OnChildCollisionEnter(BodyTypes part)
{
if(part == BodyTypes.Head)
{
//If collision hit the head
}
if (part == BodyTypes.Body)
{
//If collision hit the body
}
if (part == BodyTypes.Legs)
{
//If collision hit the legs
}
if (part == BodyTypes.Arms)
{
//If collision hit the arms
}
}
}
Your answer
Follow this Question
Related Questions
Rigidbody goes crazy when isKinematic is false 1 Answer
Ball passing through triggered collider 0 Answers
Moving a object randomly without it being inside a wall 1 Answer
How to decrease the speed of the player by some fraction after triggering the box collider? 2 Answers
Sprites not recognized as whole-spinning in different directions 0 Answers