Hi,
I’m instantiating a prefab on Key Press Z. On instantiating, I have that prefab scale indefinately as long as the key Z is pressed. On Key Up, the prefab gets destroyed.
What I require is as follows:
- When the Key is up, I need the size of prefab to which it grew to, (after 1 second) to check if any enemy falls (collides) under its area and destroy that enemy and the prefab, and add 10 points to the score.
The following is the code that I’m using
using UnityEngine;
using System.Collections;
public class InstantiateBlast : MonoBehaviour {
public GameObject smashRadius;
float speed = 3f;
private GameObject cloneRadius;
void Update (){
if(Input.GetKeyDown(KeyCode.Z))
{
//Instantiating the prefab
cloneRadius = Instantiate(smashRadius, new Vector3(0, 0, 0), Quaternion.identity) as GameObject;
//Parenting the prefab to the game object
cloneRadius.transform.parent = transform;
cloneRadius.transform.localPosition = new Vector3(0, 0, 0);
}
//Scale the object
if (Input.GetKey(KeyCode.Z)) {
if (cloneRadius != null) {
cloneRadius.transform.localScale += gameObject.transform.localScale * (Time.deltaTime * speed);
}
}
//To destroy the prefab
if(Input.GetKeyUp(KeyCode.Z))
{
Destroy(cloneRadius, 0);
}
}
}