- Home /
Shrinking an Object
I'm trying to shrink something when it comes in contact with another object and I have no idea why it isn't working. If you could help me it would be appreciated. Thanks! Here's the code that should shrink the game object:
public float targetScale = 0.1f;
public float shrinkSpeed = 2f;
gameObject.transform.localScale = Vector3.Lerp(gameObject.transform.localScale, new Vector3(targetScale, targetScale, targetScale), Time.deltaTime*shrinkSpeed);
Comment
What is the problem, doesn't it shrink at all, or does it go too fast?
It does not shrink at all and here's the full code:
public class $$anonymous$$agnetism : $$anonymous$$onoBehaviour {
public GameObject planet;
public float gravityFactor = 0.5f;
private SpriteRenderer spriteRenderer;
private Color start;
private Color end;
private float t = 0.0f;
private bool go = true;
public float targetScale = 0.1f;
public float shrinkSpeed = 2f;
void Start () {
spriteRenderer = GetComponent<SpriteRenderer> ();
start = spriteRenderer.color;
end = new Color (start.r, start.g, start.b, 0.0f);
}
void FixedUpdate(){
if (Input.Get$$anonymous$$ouseButton (0)) {
rigidbody2D.AddForce ((planet.transform.position - transform.position).normalized * rigidbody2D.mass * gravityFactor / (planet.transform.position - transform.position).sqr$$anonymous$$agnitude);
}
}
void OnTriggerEnter2D(Collider2D coll){
if (coll.gameObject.tag == "Player") {
StartCoroutine(fade ());
}
}
IEnumerator fade(){
while (go == true){
gameObject.transform.localScale = Vector3.Lerp(gameObject.transform.localScale, new Vector3(targetScale, targetScale, targetScale), Time.deltaTime*shrinkSpeed);
t += Time.deltaTime;
renderer.material.color = Color.Lerp (start, end, t / 1f);
if (renderer.material.color.a <= 0.0)
Destroy (gameObject);
yield return null;
}
}
}
Best Answer
Answer by ExtremePowers · Nov 12, 2014 at 10:02 PM
Try this:
private float curScale;
public float startScale;
public float shrinkSpeed;
void Update() {
curScale = Mathf.MoveTowards(curScale, startScale, Time.deltaTime * shrinkSpeed);
gameObject.transform.localScale = new Vector3(curScale, curScale, curScale);
}
With that bit of code it doesn't do anything at all anymore.