• 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 Bentoon · Dec 13, 2020 at 04:55 AM · gameobjectscaleplayer movement

Game Object Scaling based on Proximity

helloHello,

I want to scale a Game object based on how far away on how close the player is.

Note This is eventually for VR. I have a Game Object thats a small room and as you get closer it scales to be larger & visVersa.

What am I doing wrong?

 public class scalePlayer : MonoBehaviour
 {
     public GameObject room;
     public Transform center;
     public float oldDist;
 
     void Awake()
     {
    //Determine the Starting Distance between player and center of room to be scaled
          float oldDist = Vector3.Distance(center.position, transform.position);
     }
 
 
 
     void Update()
     {
             //Determine the New distance between player and center of room to be scaled
             float newDist = Vector3.Distance(center.position, transform.position);
             room.transform.localScale = room.transform.localScale * (oldDist - newDist) * Time.deltaTime;;
        }
 }
 

There has to be an elegant way to do this

The scale of the room just goes down to Zero

Any ideas?

Thanks!

~be

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

2 Replies

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

Answer by Hellium · Dec 13, 2020 at 07:50 PM

You were on the right track actually. Lenda0210 is right indicating you should remove the Time.deltaTime. If you want to scale down the closer the object is from the center (if the obejct is at center's position, the object will have a scale of (0,0,0):

   public class scalePlayer : MonoBehaviour
   {
       public GameObject room;
       public Transform center;
       public float ScaleMultiplier;
       private Vector3 initialScale;
   
       void Awake()
       {
               initialScale = room.transform.localScale;
       }
   
       void Update()
       {
               float distance = Vector3.Distance(center.position, transform.position);
               room.transform.localScale = initialScale * distance * ScaleMultiplier;
       }
   }


If you want the room to have its initial size when the object is on the same position as the center:

       void Update()
       {
               float distance = Vector3.Distance(center.position, transform.position);
               room.transform.localScale = initialScale + Vector3.one * distance * ScaleMultiplier;
       }
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 Bentoon · Dec 13, 2020 at 09:46 PM 1
Share

@Hellium Awesome! Thank you ! Since I want make the Object Larger as you approach I modded the code like such:

  public class scalePlayer : $$anonymous$$onoBehaviour
   {
        public GameObject room;
        public Transform center;
        public float Scale$$anonymous$$ultiplier;
        private Vector3 initialScale;
    
        void Awake()
        {
                initialScale = room.transform.localScale;
        }
    
        void Update()
        {
                float distance = Vector3.Distance(center.position, transform.position);
                room.transform.localScale = (initialScale / distance) * Scale$$anonymous$$ultiplier;
        
 
        }
    
   }


Thanks both @Lenda0210 & @Hellium

avatar image
0

Answer by Lenda0210 · Dec 13, 2020 at 07:50 AM

Just remove the Time.delta time and should be fine ^-^ If multiplier equals to one, it will be increasing 1 scale per distance (meters maybe? The unity base distance unit idk). Hope this helps you!

  public class scalePlayer : MonoBehaviour
  {
      public GameObject room;
      public Transform center;
      public float ScaleMultiplier;
  
      void Awake()
      {
 
      }
  
      void Update()
      {
              float Dist = Vector3.Distance(center.position, transform.position);
              room.transform.localScale = Dist * ScaleMultiplier;
         }
  }
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 Bentoon · Dec 13, 2020 at 07:18 PM 0
Share

@Lenda0210 Thanks but the "room" Just gets big really fast and doesn't come back down when I move away

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

223 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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 to get an object to fill 3/4th of the screen. 0 Answers

How to scale gameobject width with camera? 0 Answers

Make gameobject size always be the same 3 Answers

How to allow player controller to move game objects 2 Answers

Extend only a part of an object unity 1 Answer

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