- Home /
Increasing an GamObjects transform.localScale by 1 with OnTriggerStay
I want a trigger to increase its size once a collider "collides" with it, the problem im having is that the whole time the collider is "colliding" with the trigger the size increases. I want it to increase by 1 unit, then if another collider makes contact increase by a further 1 unit etc.
var xzlen = 1;
function OnTriggerStay(other : Collider){
var myTrig = GameObject.FindWithTag("myTrigger");
myTrig.transform.localScale = Vector3(xzlen,1,xzlen);
if(other.CompareTag("myCollider")){
xzlen++;
}
}
Answer by KeithK · Apr 05, 2011 at 03:19 PM
Use OnTriggerEnter instead of Stay.
Enter is called once as a collision begins, Stay is called while the objects are colliding and Exit is called once when the object stops colliding.
function OnTriggerEnter(other : Collider)
{
if (other.CompareTag("myCollider"))
{
transform.localScale += Vector3(1, 0, 1);
}
}
That if statement is only necessary if you are expecting it to collide with something else that you don't want to trigger a size increase. If not you can strip the if out.
This script will increase the scale of your trigger in X and Z axis by 1 for each instance of a collider that collides with it (if the collider is tagged "myCollider").
To Lerp from the current scale to your target scale, you can use the following script. Send however many seconds you'd like the interpolation to take as the parameter.
function OnTriggerEnter(other : Collider) { // I've left out checking for the collider, if you need it add it.
StartCoroutine(LerpScale(2.0f)); }
function LerpScale(time : float) { var originalScale = transform.localScale; var targetScale = originalScale + Vector3(1.0f, 0.0f, 1.0f); var originalTime = time;
while (time > 0.0f) { time -= Time.deltaTime;
transform.localScale = Vector3.Lerp(targetScale, originalScale, time / originalTime);
yield;
} }
That should do it. The divide in the Lerp is necessary to have times longer than 1 second. I hope I have all the JavaScript stuff correct.
The problem is it only increases with one collision of an instantiated prefab, I want the object to grow up by 1 with every collision.
Well the problem sounds like a step towards what you want? Try my edited script and see what happens with that.
That's perfect, don't see how I didn't work that out :D! cheers
Could I ask how you would go about making a Vector3.Lerp from this, on my attempt there was no scaling what so ever!
Sure. I'll update my post, you may want to update your question title as well to something more fitting. And don't forget to vote up and mark as answer answers that sort you out. =)
Answer by Zennig · Jan 20, 2015 at 05:17 PM
I know it is already answered; Alternatively, you can set integer variable change the Vector3 values instead of creating new Vector3 to add up. This worked out for me; I have no idea the performance difference between those. Again, just sharing an alternative;
//declaring variables...
private int xValue;
private int zValue;
//assigning initial values to each value or collect from the gameobject...
xValue = transform.localScale.x;
zValue = transform.localScale.z;
function OnTriggerEnter(other : Collider)
{
if (other.CompareTag("myCollider"))
{
xValue ++
zValue ++
transform.localScale = Vector3(xValue, 0, zValue);
}
}
Your answer
Follow this Question
Related Questions
Can't click gameobject when over another trigger? 1 Answer
OnTriggerStay Question 0 Answers
Ask for trigger-collision inside Update() 1 Answer
OnTrigger event when colliders are already touching eachother 1 Answer
Combining Triggers 1 Answer