I’m trying to make a gun that the bullets growing in size while you hold down the fire key i got it working by just creating multiple instances of the object but i cant figure out how to destroy them after the next object is place. is there a better to scale it as to not run into this problem?
heres my code:
using System.Collections;
using UnityEngine;
public class gunfire : MonoBehaviour
{
public Rigidbody projectile;
public Transform chargeSpawn;
public GameObject bubble;
public Transform spawnpoint;
public bool isGrowing = false;
IEnumerator growCoroutine;
void Start()
{
growCoroutine = growCor();
}
private void Update()
{
if (Input.GetMouseButtonDown(0))
growStart();
if (Input.GetMouseButtonUp(0))
growStop();
}
IEnumerator growCor()
{
while (isGrowing) {
Instantiate(bubble, spawnpoint.position, spawnpoint.rotation);
bubble.transform.localScale += new Vector3(0.025f, 0.025f, 0.025f);
var radius = transform.localScale.y;
if (radius == 3.7) {
growStop();
}
yield return null;
}
}
void growStart() {
if (!isGrowing) {
isGrowing = true;
StartCoroutine(growCoroutine);
}
}
void growStop() {
if (isGrowing)
{
StopCoroutine(growCoroutine);
isGrowing = false;
bubble.transform.localScale = new Vector3(1f, 1f, 1f);
var radius = transform.localScale.y;
float speed = (100 / radius);
Rigidbody InstantiateProjectile = Instantiate(projectile, transform.position, transform.rotation) as Rigidbody;
InstantiateProjectile.velocity = transform.TransformDirection(new Vector3(speed, 0, 0));
}
}
}