Hi guys!
Sorry for this question but, i’m already seeing a lot of tutorials and none as useful yet. So… I have my gaming area set in:
xMin = -10
xMax = 10
yMin = -4.3
yMax = 4.3
And i have my collider code:
public GameObject explosion; // Prefab of explosion effect.
void Start ()
{
}
void OnExplode()
{
// Create a quaternion with a random rotation in the z-axis.
Quaternion randomRotation = Quaternion.Euler(0f, 0f, Random.Range(0f, 360f));
// Instantiate the explosion where the rocket is with the random rotation.
Instantiate(explosion, transform.position, randomRotation);
}
void OnTriggerEnter2D (Collider2D col)
{
// If it hits an enemy...
if(col.tag == "Inimigo")
{
// ... find the Enemy script and call the Hurt function.
//col.gameObject.GetComponent<Inimigo>().Hurt();
// Call the explosion instantiation.
OnExplode();
// Destroy the rocket.
Destroy (gameObject);
}
// Otherwise if the player manages to shoot himself...
else if(col.gameObject.tag != "Player")
{
// Instantiate the explosion and destroy the rocket.
OnExplode();
Destroy (gameObject);
}
}
I want to know if there is a way to know when my object left the gaming area he can be destroyed. I try to pick up his position and compare with a vector3 but doesn’t work
This is the code where i try to compare the x Axis from a variable gArea (Gaming Area) and position of my object instance.
void Update ()
{
gArea = new Vector3 (Mathf.Clamp(rocket.position.x, -10f, 10f), Mathf.Clamp(rocket.position.y, -4.3f, 4.3f), 0.0f);
// If the fire button is pressed...
if(Input.GetButtonDown("Fire1"))
{
rocket.transform.localScale = new Vector3(xScale, yScale, 0.0F);
// ... instantiate the rocket facing right and set it's velocity to the right.
Rigidbody2D bulletInstance = Instantiate(rocket, transform.position, Quaternion.Euler(new Vector3(0,0,0))) as Rigidbody2D;
bulletInstance.velocity = new Vector2(speed, 0);
//Compare x position to destroy the object
if (bulletInstance.position.x == gArea.x)
{
Destroy(rocket);
}
}
}
Somebody can help me ?