How can I protect myself with an object (shield) from the impact of another object (rocket)?

1

What I want to achieve is to activate a shield with colliders or triggers, with the left mouse button "Fire 2" when a rocket hits me and does not destroy me.

    
asked by Anónimo 21.12.2016 в 13:08
source

2 answers

0

Assuming that the rocket is launched correctly, that it is a rigidbody to which you give impulse and that the shield is an object that is referenced from the Inspector panel, the approach of class quitarshieldlollevaelescudo seems correct, but I see some errors that will prevent you from executing the code. For starters, you have a public field called escudo , but then, apparently, you refer to it by the name shield . Also, you are missing a parenthesis in that if statement.

public class quitarshieldlollevaelescudo : MonoBehaviour {
    public GameObject escudo;    

    // Update is called once per frame
    void Update () {
        if (Input.GetButton("Fire2"))
            escudo.SetActive(true);
        // Te falta desactivarlo cuando no pulsan el botón
        else
            escudo.SetActive(false);
    }
}

Now, you should add a script to the shield and implement the OnCollisionEnter() method to destroy the rocket every time it collides with it. Add a "missile" tag to the rocket prefab and do something like this:

private void OnCollisionEnter(Collision col)
{       
    if (col.gameObject.tag == "missile") 
    { 
        // TODO: Activas efecto de partículas y sonido de explosión
        // Destruyes el misil
        Destroy(col.gameObject);
    }     
}
    
answered by 21.12.2016 / 15:23
source
0

What if while the secondary click is pressed, you divert the damage caused? I mean, when the rocket hits the player, it generates a damage, which is directly related to the player's life, if the player has pressed the secondary click, this damage is no longer related to the player's life, and simply makes the action to explode, I have unity installed at this time so I can only help you with theory :)

    
answered by 21.12.2016 в 13:57