Trigger only if hit hard enough (not sure if possible)

Hey guys, i’m very new to unity and coding in general, i have to make a barrel explode, but only if it’s hit hard enough by our player. currently i have it set, so it explodes if the player hits it at all.

Any ideas how i could change this?

Thank you.

This is the code i have so far:

    public LayerMask PlayerMask;
    public ParticleSystem ExplosionParticles;
    //public AudioSource ExplosionAudio;
    public float MaxDamage = 25f;
    public float ExplosionForce = 1000f;
    public float MaxLifeTime = 2f;
    public float ExplosionRadius = 5f;
    public int Velocity = 0;
    private void Start()
    {
        //Destroy(gameObject, MaxLifeTime);
    }


    void Update()
    {
       
    }



    private void OnTriggerEnter2D(Collider2D other)
    {
        if (other.gameObject.tag == "Player")
        {
            Collider[] colliders = Physics.OverlapSphere(transform.position, ExplosionRadius, PlayerMask);

            for (int i = 0; i < colliders.Length; i++)
            {
                Rigidbody2D targetRigidbody = colliders[i].GetComponent<Rigidbody2D>();

                if (!targetRigidbody)
                    continue;

                //float damage = CalculateDamage(targetRigidbody.position);

                //targetHealth.PlayerDamage(damage);
            }

            ExplosionParticles.transform.parent = null;
            ExplosionParticles.gameObject.SetActive(true);

            ExplosionParticles.Play();

            //ExplosionAudio.Play();


            Destroy(ExplosionParticles.gameObject, ExplosionParticles.duration);
            Destroy(gameObject);
        }
    }

what is “hard enough” ? :smile:
you can try to check moving speed from the player:

 if (playersRigidbody.velocity.magnitude > hardEnoughSpeed)
        {
            //explosion
        }
2 Likes

no clue what “hard enough” is yet :smile:

hmm it gives me a compiler error when i try to put that in, i put it under the other if statement in line 24.

“Assets\Scripts\TNTExplosion.cs(32,30): error CS1061: ‘object’ does not contain a definition for ‘velocity’ and no accessible extension method ‘velocity’ accepting a first argument of type ‘object’ could be found (are you missing a using directive or an assembly reference?)”

Might be the wrong place to put it tho ^^ ,

try to get rigidbody:

if (other.gameObject.GetComponent<Rigidbody2D>().velocity.magnitude > neededSpeed)

YES! i made that work! you are a literal god! thank you so much!

lol, don’t make too much barrels kaputt :smile:

nah is supposed to be around 20 barrels for a whole level max. :slight_smile: but it just felt a lot better that they could be poked a bit before actually exploding, so this was a major help

can be that you should use scrMagnitude instead of magnitude (not so cpu-expensiv).

and if the barrels can be moved (with physic), then you can check their speed too

1 Like

“Hard enough” would be your speed or velocity. So if you’re constantly fluctuating speed/velocity then all you need to do is set the max limit allowed before triggering your event. @vakabaka has provided you with the means to detect. However, instead of using the magnitude, try using your current speed/velocity instead. I don’t think it is as expensive and you’ll not be as limited. As far as the physics are concerned, I think the magnitude may be the better approach. But you could just AddForce() and create your own variation, no?