Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Rxanadu · Nov 24, 2013 at 02:33 AM · ontriggerenteroncollisionenterlocalscalevector3.lerpmagnitude

Scaling objects over time

I'm having issues properly scaling object in my game. Specifically, I'm trying to scale my player to a certain amount when he or she collides with certain objects. For example, colliding into a ball will cause the player to scale down to half its normal size; meanwhile, colliding into a square will scale the player to twice its size.

As of right now, I've set up a single function to scale the player based on a value other objects set as one of the function's parameters. When I first made the function, I initially mistook the magnitude to be a single value from the Vector3 I need to create inside the function. Here is the function in full:

 //*used in other scripts
     //changes player's scale based on amount used for each player 
     public void AlterPlayerScale(float scaleValue, float initMagnitude, float scalingDuration){
         //set scaling amount for scaling player    
         Vector3 newScale = new Vector3(initMagnitude+scaleValue, 
                                        initMagnitude+scaleValue, 
                                        initMagnitude+scaleValue);
         Debug.Log ("new Scale: "+ newScale);
         
         //scaling step
         scaleStep += Time.deltaTime/scalingDuration;
         Debug.Log ("player's scale: " + transform.localScale.x);
         
         //alter player's scale
         transform.localScale = Vector3.Lerp(transform.localScale, 
                                                    newScale, 
                                                    scaleStep);
     }

I have two objects that alter the player's scale: a collectable, which should increase the player's scale; and a enemy, which decreases the player's scale. Both objects have scripts referencing the function above via OnTriggerEnter() or OnCollisionEnter(), respectively: when the objects collides with the player, the object finds the player's current local scale magnitude to compare with in the AlterPlayerScale() function above.

However, an issue occurs when the player collides with an enemy after colliding with a collectable: the vector the enemy calculates causes the function to create a positive vector.

I've thought of just using one of the three coordinates from a minimum player scale I've set up in another script to use for the 'initMagnitude' variable instead. However, the vector that comes out as a result will not be relative to the player's current scale.

As of right now, I still need help scaling the player down properly even after colliding with a collectable. If my description of the problem was confusing, please let me know so I may clarify the problem ASAP.

Comment
Add comment · Show 2
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 robertbu · Nov 24, 2013 at 05:13 AM 0
Share

I'm trying to puzzle out the correct behavior for your game. I'm visualizing three states, 'small', 'normal', and 'large'. I'm I'm visualizing that 'collectables' increase the size, so if the size is 'normal', it becomes 'large'. If the size is 'small', it becomes 'normal.' Likewise, if the player collides with an enemy, the scale is reduced by one state...'large' becomes 'normal', and 'normal' becomes small. Is this the behavior you are looking for?

avatar image Rxanadu · Nov 25, 2013 at 08:33 PM 0
Share

It wasn't entirely how I envisioned it when I made the script during a game jam, but now that I think of it, this is the basic philosophy. The way the script works now relies too heavily on math to get the scaling to work properly.

For example, if the player (given a maximum scale of 5 units, a $$anonymous$$imum scale of 1 unit, and a current scale of 2) touches a collectable, the player should scale from 2 to 3; touching an enemy should scale the down to 1.

If I were to adopt your states model, I would still have to find out how to scale the player based on which object he or she touches.

2 Replies

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

Answer by robertbu · Nov 25, 2013 at 09:10 PM

There are a couple of ways of looking at this problem. You can do as you've done and have the object that your player collide with tell the player to grow or shrink based, or you could have the player recognize what it is colliding with and do the right thing. I would lean towards the latter, but since your code does the former, here is a bit of untested code outline how I might approach the problem:

 using UnityEngine;
 using System.Collections;
 
 public class Scaling : MonoBehaviour { 
     public int startSize = 3;
     public int minSize = 1;
     public int maxSize = 6;
     
     public float speed = 2.0f;
     
     private Vector3 targetScale;
     private Vector3 baseScale;
     private int currScale;
     
     void Start() {
         baseScale = transform.localScale;
         transform.localScale = baseScale * startSize;
         currScale = startSize;
         targetScale = baseScale * startSize;
     }
     
     void Update() {
         transform.localScale = Vector3.Lerp (transform.localScale, targetScale, speed * Time.deltaTime);
         
         // If you don't want an eased scaling, replace the above line with the following line
         //   and change speed to suit:
         // transform.localScale = Vector3.MoveTowards (transform.localScale, targetScale, speed * Time.deltaTime);
 
         if (Input.GetKeyDown (KeyCode.UpArrow))
             ChangeSize (true);
         if (Input.GetKeyDown (KeyCode.DownjArrow))
             ChangeSize (false);
     }
     
     public void ChangeSize(bool bigger) {
         
         if (bigger)
             currScale++;
         else
             currScale--;
         
         currScale = Mathf.Clamp (currScale, minSize, maxSize+1);
         
         targetScale = baseScale * currScale;
     }    
 }
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 Rxanadu · Nov 27, 2013 at 05:40 PM 0
Share

It's not working as intended: the targetScale, which is the scale variable that the lerp function hinges on, is not initialized. This means targetScale is initialized to zero, causing the player's scale to lerp between the starting scale to zero.

I thought setting the targetScale to an initial value would help in this, but no luck.

From further testing, however, I've found that the script does not work at all (except to scale the player up to a starting size).

Any suggestions?

avatar image robertbu · Nov 27, 2013 at 05:48 PM 0
Share

I found a bug with targetScale and fixed it, but it should not have caused a complete failure of the code. I added Up and Down arrow input so you can test the code. Start with an empty scene and put this script on a cube. Run the scene and use the arrow keys to increase and decrease the size of the cube.

avatar image Rxanadu · Nov 27, 2013 at 11:36 PM 0
Share

I just got a barrage of stack overflow errors after adding your alterations to the script, and now the project won't even open anymore.

avatar image robertbu · Nov 28, 2013 at 12:04 AM 0
Share

Before you add it to your project, start with a new scene, create a block, add the script to the block, run the app, and use the up and down arrow keys to size the block. As for your stack overflow problem, I'd have to see your code to give you feedback.

avatar image Rxanadu · Nov 29, 2013 at 03:27 PM 0
Share

I just tried it on a test project, and it works (generally) as intended. I'm still not sure why you use Time.deltaTime for the lerp step, as it causes the scale to reach the end of the lerp slower than having a simple step going from 0 to 1.

I've actually seen this in almost all of the tutorials online talking about lerping between two variables. Am I missing something here?

Show more comments
avatar image
0

Answer by NiaG-A · Aug 19, 2017 at 08:50 AM

I have a YouTube video about this https://www.youtube.com/watch?v=Wymchsif758 it only requires a few lines of code

Comment
Add comment · Show 1 · 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 RetroboltGames · Jun 04, 2019 at 05:07 AM 0
Share

Not anymore?

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

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Looking for a contact.point alternative with triggers 0 Answers

rigid body isKinematic not triggering as expected? 2 Answers

Multiple OnTriggerEnter on one script? 2 Answers

How to get expanding object collide when it touches others? 1 Answer

OnTriggerEnter() and OnCollisionEnter(), OnControllerColliderHit() not working with character controller 2 Answers


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