Hitbox reaction for shooter games

I am creating a collider for bounding box and some trigger collision for hitbox. I am using raycast from attacker object to the victim target. I must check if my raycast hit the victim’s hitbox, victim affected damage. I don’t know how to do because raycast called on attacker but hitscan should be in victim because trigger collision and variable health operates here. Any ideas?

There are two common operations in damage dealing - send damage and receive damage. Sending entirely happens on attacker side. It collect self stats, equip and weapon properties to determine damage amount and parameters, while receiving damage is solely victim responsibility. When attacker’s raycast hist the victim, you have both attacker and victim game object references. What exactly stops you from sending dama from attacker and receiving it on the victim?

1 Like

From your Raycast you should get a RaycastHit object. You can check the collider’s gameObject to see if you hit the victim.

// Do something like this
RaycastHit hitInfo;

if (Physics.Raycast(..., out hitInfo))
{
  if (hitInfo.collider != null && hitInfo.collider.gameObject == myVictimGameObj)
  {

  }
}
1 Like

to avoiding bug or exploit? if I want to upgrade it to multiplayer online, I thought it’s better to make hitscan method on victim and trigger it rather than call reference victim and modify it inside attacker. As far as I can do I am using reference, but I am not sure it’s okay or nah.

Yeah, I use layermask like these:

code

public void Shoot()
        {
            for (int i = 0; i< particles.Length; i++)
            {
                particles[i].Play();
            }
            audio.Play();
           
            Vector3 muzzle = particles[0].gameObject.transform.position;
            Vector3 dir = particles[0].gameObject.transform.TransformDirection(Vector3.forward);
            Debug.DrawRay(muzzle, dir, Color.red, 10f);
            if (Physics.Raycast(muzzle, dir, out hit, weapon.distance, layerHitbox))
            {
                if (hit.collider.isTrigger)
                {
                    Debug.Log("Kena!!" + hit.transform.gameObject.name + "!");
                    victimStates = hit.transform.root.GetComponent<StatesManager>();
                    if(victimStates!=null)
                        victimStates.takeDamage(weapon.damage);
                    if (hit.collider.attachedRigidbody)
                    {
                        hit.collider.attachedRigidbody.AddForceAtPosition(dir * weapon.damage * 25f, hit.point);
                        hit.collider.transform.position = dir * weapon.damage * 25f + hit.point;
                    }
                    //hit.transform.gameObject.GetComponent<Rigidbody>().AddForce(dir * weapon.damage);
                }
                else
                {
                    victimStates = hit.transform.GetComponent<StatesManager>();
                    if (victimStates != null)
                        victimStates.takeDamage(weapon.damage);
                    Debug.Log("Kena " + hit.transform.gameObject.name + "! Direction: "+dir);
                    if(hit.collider.attachedRigidbody)
                        hit.collider.attachedRigidbody.AddForceAtPosition(dir * weapon.damage *25f, hit.point);
                }
            }
        }