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.
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.
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);
}
}
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 :)