Changing the size of a ball, problems with transform.localScale
I'm trying to make it so the player object, a ball, decreases in size as a timer goes down. It's supposed to be like a snowball melting. I've made a variable to represent the ball's size so it can change as the timer changes. I've tried using transform.localScale a few different ways but the ball is always increasing in size rapidly instead of decreasing slowly. I've been trying to avoid posting it on here because I'm a newbie developer and I wanted to try and figure it out on my own. But I'm stuck on this. Any help would be greatly appreciated.
#pragma strict
var maxFallDistance : int = -10;
var ballSize:int;
var timer : float = 10.0;
function Update () {
if (transform.position.y <= maxFallDistance){
Application.LoadLevel ("Tutorial");
}
ballSize = timer;
Debug.Log (ballSize);
timer -= Time.deltaTime;
if(timer <= 0){
timer = 0;
}
transform.localScale += new Vector3(ballSize,ballSize,ballSize);
}
function OnGUI(){
GUI.Box(new Rect(10,10,50,20), "" + timer.ToString("0"));
}
Also please don't refer me to http://docs.unity3d.com/ScriptReference/Transform-localScale.html. I tried it and tweaked it a bunch of ways but it kept making it bigger.
Thanks guys -Nix
Answer by OncaLupe · Nov 11, 2015 at 04:22 PM
You're using += to change the scale, this adds to what is already there. If you start with scale of <1,1,1> and use that with ballSize 0.9, you end up with <1.9, 1.9, 1.9>. You want to use = to assign a new value instead.
transform.localScale = new Vector3(ballSize,ballSize,ballSize);
Thank you. That worked. I kept trying to use -= ins$$anonymous$$d of +=. I didn't think to just leave it as =. It's working now. Not the smoothest transition between size intervals but I'll work on that later. Thanks for your help.
Your answer
Follow this Question
Related Questions
Hosting both WebPlayer and WebGL builds on the same page (Unity3d 5.3.4f1). 0 Answers
Trying to use coroutines in order to use Mathf.Lerp, Lerp instantly jumps values 0 Answers
Can I use an array to determine an enclosed area and remove objects inside? 0 Answers
Set variables in an array dependent on which scene the player is in. 1 Answer
Reloads correctly first time, but can't reload till count hits 0 1 Answer