Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by hobocampus · Oct 02, 2015 at 03:34 AM · transformlocalscaleonmouseovervector3.distanceinput.getbutton

Scale object when player is within a given distance of it

What I'm doing with this script is scaling a cube along the y axis (increasing with left mouse button and decreasing with right mouse button) while the player is in close proximity of it (between 2 and .1 units). I want the player to be able to be on top of the cube as it's being scaled. I have the script attached to the cube and the player controller attached to the player variable in the inspector.

It was working like I wanted all day today, but now it's not. Now, the cube is being scaled even when the player isn't near it. I tried starting a new project; it worked briefly and then stopped. Any suggestions as to why this is happening? I don't believe I changed anything, but I'm a total noob, so I could've done something and didn't realize it.

I'm not really looking for any fixes to my script, although any suggestions to make it more efficient would be greatly appreciated. I would just like to figure out where I'm going wrong since, like I said, I had it working earlier.

  var player : Transform;
  
  function OnMouseOver ()
  {
  
      if(Input.GetButton("Fire1") && (Vector3.Distance(transform.position, player.position) < 2))
      {
          transform.localScale += new Vector3(0F, .1F, 0);
      }
  
      if(Input.GetButton("Fire1") && (Vector3.Distance(transform.position, player.position) > .1))
      {
          transform.localScale += new Vector3(0F, .1F, 0);
      }
  
      if(Input.GetButton("Fire2") && transform.localScale.y > 1f && (Vector3.Distance(transform.position, player.position) < 2))  
      {
          transform.localScale += new Vector3(0F, -.1F, 0);
      }
  
      if(Input.GetButton("Fire2") && transform.localScale.y > 1f && (Vector3.Distance(transform.position, player.position) > .1))
      {
          transform.localScale += new Vector3(0F, -.1F, 0);
      }
  }
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

Answer by Oribow · Oct 02, 2015 at 09:38 PM

  if(Input.GetButton("Fire1") && (Vector3.Distance(transform.position, player.position) > .1))
           {
               transform.localScale += new Vector3(0F, .1F, 0);
           }
           if(Input.GetButton("Fire2") && transform.localScale.y > 1f && (Vector3.Distance(transform.position, player.position) > .1))
           {
               transform.localScale += new Vector3(0F, -.1F, 0);
           }

These two lines doesn't make sense. Why do you allow the player to scale the object, if he is at least 0.1 distance away? I guess you wanted to prevent the player from scaling, if he is to near. But then you gotta connect both resctrictions:

 if (Input.GetButton("Fire1") && Vector3.Distance(transform.position, player.position) < 2 && Vector3.Distance(transform.position, player.position) > .1)
         {
             transform.localScale += new Vector3(0F, .1F, 0);
         }
 
         if (Input.GetButton("Fire2") && transform.localScale.y > 1f && Vector3.Distance(transform.position, player.position) < 2 && (Vector3.Distance(transform.position, player.position) > .1))
         {
             transform.localScale += new Vector3(0F, -.1F, 0);
         }


Comment
Add comment · Show 2 · 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 hobocampus · Oct 03, 2015 at 04:24 AM 0
Share

@Oribow

Thanks for the suggestion. Sorry if I didn't make myself clear.

The game object being scaled acts as a sort of elevator that the player needs to be on top of; as it is being scaled, the player ascends and descends with it. The problem I run into is that without a $$anonymous$$imum distance, the game object stops scaling at 2 units. I figured a relatively small unit (like .1) would allow the game object to be scaled past 2 units while the player is on top if it. But now I see, that doesn't really work either way.

What I need is for the player to be able to be x distance (relatively short) from the object to perform the mouse click to make the object begin to scale, but also need the object to be scaled greater than y distance (relatively long).

avatar image Oribow hobocampus · Oct 03, 2015 at 02:18 PM 0
Share

I would suggest you to make an trigger above your elevator plattform. So the player can only scale the elevator, if he's in the trigger box.

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

31 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

Related Questions

How can i reset position of my obstacle?? 0 Answers

NetworkTransform not syncing localScale 2 Answers

LocalScale not working? 4 Answers

Vector3.Distance returning offset values due to down force interference. 1 Answer

Saving default scale of an object c# 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