- Home /
Question by
unity_i0W6HQ-YEHqs4Q · Nov 06, 2019 at 02:39 PM ·
scalingscrpting
how to properly scale gameobject in script
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; using UnityEngine.UI;
public class gunfire : MonoBehaviour { public Rigidbody projectile; public Transform chargeSpawn; public GameObject bubble; private float chargeTime = 0; private float chargeRate = 2f;
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));
}
}
}
Comment