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 Deadshot1994 · Jul 04, 2019 at 01:26 PM · mouseobjectsresize

Scaling an Object with mouse

Hi so I'm not quite sure what to do, I'm trying to make an object scale while another object is being dragged by the mouse.

Here's my snippet:

  private GameObject mainObject;
     private float positionZ = 5.0f;
     public float sizingFactor = 0.03f;
     private float startSize;
     private float startX;
     private float startY;
     private float startZ;
 
     // Use this for initialization
     void Start()
     {
         mainObject = GameObject.Find("Cube");
     }
 
     // Use this when user has clicked on object and still holding it
     void OnMouseDrag()
     {
 
         //When user has left clicked
         if (Input.GetMouseButton(0))
         {
             //Move the blue box by mouse
             Vector3 positionRight = mainObject.transform.position;
             Vector3 mousePosition = new Vector3(Input.mousePosition.x, Input.mousePosition.y, Input.mousePosition.z);
             startX = mousePosition.x;
             startY = mousePosition.y;
             startZ = mousePosition.z;
             Vector3 objPosition = Camera.main.ScreenToWorldPoint(mousePosition);
             //Set the blue box by x axis of mouse and y,z axises of empty-object right one
             transform.position = new Vector3(objPosition.x, objPosition.y, objPosition.z);
             startSize = mainObject.transform.localScale.x;
         }
 
         if (Input.GetMouseButton(0))
         {
             //Scale green box by blue box
             Vector3 scale = mainObject.transform.localScale;
             scale.x = startSize + (Input.mousePosition.x - startX) * sizingFactor;
             Debug.Log(scale.x);
             scale.y = startSize + (Input.mousePosition.y - startY) * sizingFactor;
             scale.z = startSize + (Input.mousePosition.z - startZ) * sizingFactor;
             Debug.Log(scale.z);
             mainObject.transform.localScale = scale;
 
         }
 
     }
 
     void OnMouseUp()
     {
     }

But when I test it, nothing happens. Any help would be kindly appreciated!

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 Deadshot1994 · Jul 05, 2019 at 10:28 AM

I've successfully made the code to work and scale the object on all 3 axis by parsing the x axis as value.

 Vector2 prevMousePosition;
     private GameObject mainObject;
     public float sizingFactor = 0.03f;
     Vector3 minimumScale;
 
     private void Start()
     {
         // Parsing the object we want to modify to mainObject
         mainObject = GameObject.Find("Cube");
         // Setting the minimum scale of the mainObject
         minimumScale = new Vector3(1.0f, 1.0f, 1.0f);
     }
 
     void OnMouseDrag()
     {
         Vector2 mousePosition = Input.mousePosition;
 
         if (Input.GetMouseButton(0))
         {
             // Set this object's position to where the mouse is being held down by the left click.
             transform.position = Camera.main.ScreenToWorldPoint(mousePosition);
 
             // Change the scale of mainObject by comparing previous frame mousePosition with this frame's position, modified by sizingFactor.
             Vector3 scale = mainObject.transform.localScale;
             scale.x = scale.x + (mousePosition.x - prevMousePosition.x) * sizingFactor;
             scale.y = scale.x;
             scale.z = scale.x;
             mainObject.transform.localScale = scale;
 
             // Checking if current scale is less than minimumScale, if yes, mainObject scales takes value from minimumScale
             if (scale.x < minimumScale.x)
             {
                 mainObject.transform.localScale = minimumScale;
             }
         }
         
         prevMousePosition = mousePosition;
     }
 }


This now works, however there is still one problem, which is that the object immediately scales to a value of 10 on all axes, instead of waiting to scale when the dragging of the mouse begins.

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

187 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

Related Questions

Get Velocity of Rigidbody Object Below Another Object 0 Answers

How do I pass in a GameObject as a parameter when I'm instantiating a class? 0 Answers

Unintentional hidden Objects 0 Answers

How to resize terrain? 2 Answers

Characters are small in playmode 0 Answers


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