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
0
Question by semi-mong · Apr 21, 2015 at 05:57 AM · transform

Constraint on localScale - how to?

Hello,

Im looking to contrain the local scale of my 2d box, i'll post code below, but in text form, i have a 2D box, controlled by the arrows keys, UP makes it go up, RIGHT goes right etc and when the space bar is held, the arrow keys change the size of the box instead of position, i.e up increase height, down decreases, reight increases width, left decrease.

But when space bar is held and you decrease the box until it hits a zero value, it flips and instead of the left button decsreasing width, its incresaing the width of the box in the opposite direction. Can i contraint this so that when the box hits its minimum value of zero it doesnt flip and start increasing the other way.

This code is for localscale only.

Code follows.

 using UnityEngine;
 using System.Collections;
 
 public class localScale : MonoBehaviour { 

     void Start ()                         
     {
     }
 
     void Update () 
     {
 
         if (Input.GetKey (KeyCode.Space))          
         { 
             if (Input.GetKey (KeyCode.RightArrow)) 
             {
                 transform.localScale += new Vector3(0.05f,0.0f,0);
                                                                    
             }                                                         
         }                                                             
         if (Input.GetKey (KeyCode.Space))
         { 
             if (Input.GetKey (KeyCode.LeftArrow))                     
             {
                 transform.localScale -= new Vector3(0.05f,0.0f, 0 ); 
             }                                                            
         }                                                              
         
         if (Input.GetKey (KeyCode.Space))
         { 
             if (Input.GetKey (KeyCode.DownArrow))        
             {
                 transform.localScale -= new Vector3 (0.0f,0.05f, 0); 
             }                                                        
         }
         
         if (Input.GetKey (KeyCode.Space))
         { 
             
             if (Input.GetKey (KeyCode.UpArrow))           
             {
                 transform.localScale += new Vector3(0.0f,0.05f, 0 );  
             
             }
 
 
         }    
     }
 }
 

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 yashpal · Apr 21, 2015 at 10:19 AM 0
Share

@semi-mong , just use Clamp.

              //transform.localScale -= new Vector3(0.05f,0.0f, 0 );  //Your code line. delete it and use new line

     transform.localScale = new vector3( $$anonymous$$athf.Clamp( transform.localScale.x + 0.05f, $$anonymous$$inValueToScale , $$anonymous$$axValueToScale  ), transform.localScale.y,transform.localScale.z ); // new code line You need to put $$anonymous$$inValueToScale and $$anonymous$$axValueToScale .

hope this helps you.

avatar image semi-mong · Apr 21, 2015 at 01:05 PM 0
Share

Thanks this really helped! although i didn't use it in this application but will do in my next application, thanks!

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by JinJin · Apr 21, 2015 at 10:21 AM

Very simple :)

 void Update () 
  {
      if (Input.GetKey (KeyCode.Space))          
      { 
          if (Input.GetKey (KeyCode.RightArrow)) 
          {
              transform.localScale += new Vector3(0.05f,0.0f,0);                                        
          }                                                         
 
          if (Input.GetKey (KeyCode.LeftArrow) && transform.localScale.x > 0.0f)                     
          {
              transform.localScale -= new Vector3(0.05f,0.0f, 0 ); 
          }                                                            
 
          if (Input.GetKey (KeyCode.DownArrow) && transform.localScale.y > 0.0f)        
          {
              transform.localScale -= new Vector3 (0.0f,0.05f, 0); 
          }                                                         
          
          if (Input.GetKey (KeyCode.UpArrow))           
          {
              transform.localScale += new Vector3(0.0f,0.05f, 0 );  
          }
      }    
  }
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 semi-mong · Apr 21, 2015 at 01:06 PM 0
Share

Thanks! seems to be a lot friendly on the eyes and simpler for my $$anonymous$$d to get around! used it in my 2d box application !

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Transform vs Gameobject 3 Answers

seperation of prefabs with two dimensional terrain generation 0 Answers

How rotate only z axis of Gameobject (2D) 1 Answer

How to move two same Objects at the same place in different scenes? 1 Answer

moving random objects to random positions 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