• 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 MidnightCoffeeInc · Jan 05 at 12:14 AM · transformscale

Change GameObject Scale by relative value?

So I'm trying to allow the player to scale an object up and down using the left and right arrow keys.
I'm doing it with the code below, but if I keep subtracting by the same value, as the gameObject gets smaller, the scale speed will increase. Is there a way for me to have the object scale up/down keeping the same scale factor, so it increases/decreases at the same rate?

I hope this makes sense! Thanks!

 float objectScaleSpeed = 2f;
 selectedObject.transform.localScale += new Vector3(objectScaleSpeed, objectScaleSpeed, objectScaleSpeed);




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

1 Reply

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

Answer by Subcreation · Jan 05 at 01:40 AM

If I am understanding correctly, it might be possible to multiply by a scalar value instead of adding:

 float objectScaleSpeed = 2f;
 selectedObject.transform.localScale *= 1f + objectScaleSpeed * Time.deltaTime;


This is probably sufficient, but you could also give each object a script that stores a relative scale value and set the object's scale according to a formula. For example:

 Vector3 originalLocalScale;
 void Start(){
   originalLocalScale = transform.localScale;
 }
 
 float myRelativeScale = 1f;
 public void IncreaseScale(float objectScaleSpeed){
   myRelativeScale += Time.deltaTime * objectScaleSpeed;
   transform.localScale = originalLocalScale * Mathf.Pow(myRelativeScale, 1f/3f);
 }

I hope this gives you some ideas!

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 MidnightCoffeeInc · Jan 05 at 08:16 PM 1
Share

This is perfect, thank you!

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

145 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

Related Questions

Pressing 'Play' makes Unity mess with Transform properties 0 Answers

Translating measurement in rotation speed and game object size to non-technical staff 0 Answers

Warning: "Setting scale failed, ignoring request." ? 1 Answer

How to avoid scaling heritage when parenting. 3 Answers

How to preserve size of children when changing scale of parent 5 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges