Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Caiuse · Apr 05, 2011 at 03:16 PM · collidertriggerontriggerstaylocalscale

Increasing an GamObjects transform.localScale by 1 with OnTriggerStay

alt text

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++;
 }   

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
7
Best Answer

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.

Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Caiuse · Apr 05, 2011 at 04:45 PM 0
Share

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.

avatar image KeithK · Apr 05, 2011 at 04:58 PM 0
Share

Well the problem sounds like a step towards what you want? Try my edited script and see what happens with that.

avatar image Caiuse · Apr 06, 2011 at 04:55 PM 0
Share

That's perfect, don't see how I didn't work that out :D! cheers

avatar image Caiuse · Apr 06, 2011 at 05:39 PM 0
Share

Could I ask how you would go about making a Vector3.Lerp from this, on my attempt there was no scaling what so ever!

avatar image KeithK · Apr 06, 2011 at 06:25 PM 0
Share

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. =)

Show more comments
avatar image
1

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);
         }
     }



Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by smoluck · Feb 21, 2018 at 07:08 AM

Hi, i'm searching for another concept.

I wan t to scale down to 0% items that are out of a certain volume (Sphere).

Is their a way to do this ? @Caiuse @KeithK @Zennig

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges