- Home /
OnTriggerEnter and transform.localScale with Time.deltaTime problem
Hi,
I feel I am close to understanding this but can't quite nail it.
I have a OnTriggerEnter (bulletCollision:Collider) function. Inside that function I am trying to scale a gameObject using Time.deltaTime.
What happens is that the object instantly scales but not over time.
I now know that Time-deltaTime should happen in a function Update().
However, I am having difficulty in working out how to do this.
Here is an example of code I started with:
function OnTriggerEnter (bulletCollision:Collider)
{
//Detect Collision
if(bulletCollision.gameObject.name == "Bullet(Clone)"){
GameObject.Find("TargetCube(Clone)").transform.localScale += Vector3(10,10,10) * Time.deltaTime * 10;
}
}
}
After researching I have moved on to turning the scale I want to do into a Function so that I could get it to work by some how calling it in a function Update():
function OnTriggerEnter (bulletCollision:Collider)
{
//Detect Collision
if(bulletCollision.gameObject.name == "Bullet(Clone)"){
TargetCubeDeathAnim ();
}
}
}
function TargetCubeDeathAnim () {
GameObject.Find("TargetCube(Clone)").transform.localScale += Vector3(10,10,10) * Time.deltaTime * 10;
}
But I still can't work out how to try and get the scale change work in a function Update(). Any ideas?
Thanks
Paul
So to be clear you want it to scale to the same amount, but start that scaling when it hits the trigger? Not keep scaling when it is in the trigger?
Yes, that's it whydoidoit. I want to scale to a certain size over a few sceonds.
Answer by whydoidoit · Jul 01, 2012 at 06:25 PM
So you are only doing 1 scale at the moment - it's probably going to be a bit too big when you use this code because you are applying it the way you are, so adjust this to work for you.
function OnTriggerEnter (bulletCollision:Collider)
{
//Detect Collision
if(bulletCollision.gameObject.name == "Bullet(Clone)"){
yield TargetCubeDeathAnim ();
}
}
function TargetCubeDeathAnim () {
var time = 5; // 5 seconds
var go = GameObject.Find("TargetCube(Clone)");
while(time > 0)
{
go.transform.localScale += Vector3(10,10,10) * Time.deltaTime * 10; // It will get a lot bigger now!
time -= Time.deltaTime;
yield null;
}
}
Thanks for looking at this $$anonymous$$ike. I'm getting ECU0001: ';' expected. Insert a semicolon at the end. Here is the line it is refering to:
yield return TargetCubeDeathAnim ();
Hi $$anonymous$$ike, I just tried that, but get unknown identifier: 'WaitForNextFrame'.
should that be WaitForEndOfFrame ?
Sorry, just saw this! Yeah you would probably want quite small values as it is going to be the total over the period of 5 seconds...
Answer by Eric5h5 · Jul 01, 2012 at 05:29 PM
OnTriggerEnter can be a coroutine, do the scaling over time in there. No need for Update, it will just complicate things.
Thanks Eric5h5. I'm off to research coroutines. Will get back to you as soon as I have investigated which should be in the next hour or so.
Just tried coroutine (which looks very useful! and thanks for that!) but got the same result. The gameObject instantly scales up to the required size, and does not scale up over time.
To use coroutines I use yield functionnamehere(); yes?
Your answer
![](https://koobas.hobune.stream/wayback/20220613073541im_/https://answers.unity.com/themes/thub/images/avi.jpg)