Grenade deal damage don't work

Hello,i trying to make granade deal damage.First i try to make if grenade hit something to debug ,
Congralations you hit it!“.But when I throw grenade to hit cube that have box collider.It don’t calculate cube as object and don’t debug ,Congralations you hit it!” Can you help me please?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Grenade : MonoBehaviour
{
    public float delay = 3f;
    public float radius = 5f;
    public float force = 700f;
    public float damage = 10f;

    public GameObject explosionEffect;

    float countdown;
    bool hasExploded = false;

    // Start is called before the first frame update
    void Start()
    {
        countdown = delay;
    }

    // Update is called once per frame
    void Update()
    {
        countdown -= Time.deltaTime;
        if(countdown <= 0f && !hasExploded)
        {
            Explode();
            hasExploded = true;
        }
    }
   
    void Explode()
    {
        GameObject grenadeffect = Instantiate(explosionEffect, transform.position, transform.rotation);

        Collider[] colliders = Physics.OverlapSphere(transform.position, radius);

        foreach (Collider nearbyObject in colliders)
        {
            // Add Force
            Rigidbody rb = nearbyObject.GetComponent<Rigidbody>();
            if (rb != null)
            {

                RaycastHit hit;
                rb.AddExplosionForce(force, transform.position, radius);
                if(Physics.Raycast(nearbyObject.transform.position, nearbyObject.transform.forward, out hit))
                {
                    Target target = hit.transform.GetComponent<Target>();
                    if(target != null)
                    {
                        Debug.Log("Congralations you hit it!");
                    }
                }
            }
        }

        Destroy(gameObject);
        Destroy(grenadeffect, 1.5f);
    }
}

Your raycast is starting from the nearbyObject’s position and going in the direction it happens to be facing. You want to use your grenade’s information instead. Try this:

// point from the grenade toward the object
Vector3 direction = (nearbyObject.transform.position - transform.position).normalized;

if(Physics.Raycast(transform.position, direction, out hit))
1 Like

Can you enter that in my code and show me,because i don’t understand should i remove something or no.Thanks

Delete your Physics.Raycast line and put in my two lines of code.

This

    void Explode()

    {

        GameObject grenadeffect = Instantiate(explosionEffect, transform.position, transform.rotation);

 

        Collider[] colliders = Physics.OverlapSphere(transform.position, radius);

 

        foreach (Collider nearbyObject in colliders)

        {

            // Add Force

            Rigidbody rb = nearbyObject.GetComponent<Rigidbody>();

            if (rb != null)

            {

 

                RaycastHit hit;

                rb.AddExplosionForce(force, transform.position, radius);

              // point from the grenade toward the object

Vector3 direction = (nearbyObject.transform.position - transform.position).normalized;

 

if(Physics.Raycast(transform.position, direction, out hit))



                {

                    Target target = hit.transform.GetComponent<Target>();

                    if(target != null)

                    {

                        Debug.Log("Congralations you hit it!");

                    }

                }

            }

        }

 

        Destroy(gameObject);

        Destroy(grenadeffect, 1.5f);

    }

}

Btw sry if i type something wrong,i,m on phone now and is hard to type

Btw i fixed it tnx you so much ! <3

1 Like