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 OsamiWorks · Mar 20 at 11:39 AM · scripting beginnerscriptingbasicsscriptableobjectscalingscriptingproblem

Junior Programming Pathway scaling a cube

In the junior programming pathway for the "mod the cube" assignment, I'm trying to have the cube scale up incrementally by 0.1 until it reaches a certain size, then scale back down by the same increment.

My function for doing this has no errors but the result isn't what I want.

It starts the cube at 0 and scales up by 0.1 to 9.00001 which is below the lower bound of the range I've set and then it gets stuck. I've tried changing the value of scaleCube and changing the second if to an else if statement plus a few other things but it still gets stuck at the same number. Could someone help direct me towards making my function work as intended?

   {
     public MeshRenderer Renderer;
     private float scaleCube;
 
     void Start()
     {
     //default script code is here
     }
     void Update()
     {
         scaleCube = cubeScale(scaleCube);
         Debug.Log(scaleCube);
 
         transform.localScale = Vector3.one * scaleCube;
 
         transform.Rotate(10.0f * Time.deltaTime, 0.0f, 0.0f);
     }
     private float cubeScale(float x)
     {
         float maxRange = 5;
         float minRange = 1;
         float currentBound = 1;
         
         if (x <= currentBound)
         {
             x = x + 0.1f;
             if (currentBound >= maxRange)
                 {
                 currentBound = minRange;
                 }
         }
         if (x >= currentBound)
         {
             x = x - 0.1f;
             if (x <= currentBound)
             {
                 currentBound = maxRange;
             }
         }
         return x;
     }
 }
 
 







Comment
Add comment · Show 1
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 highpockets · Mar 20 at 12:31 PM 0
Share

I don’t see how this would get past a local scale of Vector3(1,1,1).

2 Replies

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

Answer by Narc0t1CYM · Mar 20 at 01:38 PM

Your loop is stuck in an infinite loop because as soon as it reaches 1.0f, it goes to your next part of code which will decrease x by 0.1f then when the code runs again it will increase it by 0.1f to 1.0f, but then it reached currentBound again so it goes on to your next part of the code which decreases x by 0.1f and so on.

What I'd suggest you having in a case like this is either a bool that is checking whether it's incrementing or not, or a more readable, elegant way - especially if your cube would have another state then increasing and decreasing - is to use an enum.

While your _scaleState is set to Increasing it will increase x by the given amount until it reaches your maxRange and then it will set _scaleState to Decreasing which will run the other part of your code that is decreasing x. Once x reaches minRange, _scaleState switches back to Increasing thus completing a full circle.

I provided you a code below that would fit this scenario:

 using UnityEngine;
 
 public class CubeScaler : MonoBehaviour
 {
     public MeshRenderer Renderer;
     private float _scaleCube;
 
     enum ScaleState
     {
         Increasing,
         Decreasing
     }
 
     private ScaleState _scaleState;
 
     void Start() {
         _scaleState = ScaleState.Increasing;
     }
 
     void Update()
     {
         _scaleCube = cubeScale(_scaleCube);
         Debug.Log(_scaleCube);
  
         transform.localScale = Vector3.one * _scaleCube;
  
         transform.Rotate(10.0f * Time.deltaTime, 0.0f, 0.0f);
     }

     private float cubeScale(float x)
     {
         float maxRange = 5;
         float minRange = 1;
 
         if (_scaleState == ScaleState.Increasing) {
             if (x >= maxRange) {
                 _scaleState = ScaleState.Decreasing;
             }
 
             x += 0.1f;
         }
         
         if (_scaleState == ScaleState.Decreasing) {
             if (x <= minRange) {
                 _scaleState = ScaleState.Increasing;
             }
 
             x -= 0.1f;
         }
         
         return x;
     }
 }
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
1

Answer by rh_galaxy · Mar 20 at 01:44 PM

This will do ping pong between scale 1 and 5 with descrete steps of 0.1 every 0.2 second (Untested). You are currently tying to change the scale with 0.1 every frame, which I suspect is not what you want, it will be changing scale to quickly.

 private float way = 1.0f;
 private float timer = 0.0f;
 private float cubeScale(float x)
 {
     timer += Time.deltaTime;
     if(timer >= 0.2f) //do scaling change in 0.2S time steps
     {
         timer = 0.0f;
         
         x += way * 0.1f;
         if(x >= 5.0f) { x=5.0f; way *= -1.0f; }
         if(x <= 1.0f) { x=1.0f; way *= -1.0f; }
     }
     return x;
 }
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

156 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 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image

Related Questions

How do I make a photography function? 0 Answers

How to instantiate Road Prefab at desired position ? 1 Answer

looking for a way to set an integer to another number based on a condition. 2 Answers

Trying to find the highest number than add it to itself. 2 Answers

How to get multiple string values from override ToString()? 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