• 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 anvi · Aug 14, 2012 at 08:48 PM · mousedragscaling

How can i make a gameObject Scale/Resize with mouse drag

Hi all,

I want to make a gameObject (a Plane or cube) to scale while i press and drag the left arrow mouse down. Is this possible? Can you point out best practices or tutorials or working script. Plz help me m new to scripting and i need ur help.

thanks in advance.

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 flaviusxvii · Aug 14, 2012 at 10:10 PM 0
Share

Learn by doing. Find a tutorial on responding to mouse events (particularly drag). Find a tutorial on scaling objects. Link that stuff together. BA$$anonymous$$.

avatar image Loius · Aug 15, 2012 at 12:35 AM 0
Share

To expand on that, start with one of the OFFICIAL unity tutorials. Youtube is great for some things but I have yet to see a Youtube tutorial actually explain what's going on. Look under the Support link along the top of the page, that's where they're hidden.

1 Reply

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

Answer by $$anonymous$$ · Aug 15, 2012 at 09:58 AM

Create a script called "ResizableObject" and add it to your object.

 using UnityEngine;
 using System.Collections;
 
 public class ResizableObject : MonoBehaviour 
 {
  
 }

And Then create a script called "InputHandler" and create a new gameObject and add this script to it.

 using UnityEngine;
 using System.Collections;
 
 public class InputHandler : MonoBehaviour 
 {
  private Ray m_Ray;
  private RaycastHit m_RayCastHit;
  private ResizableObject m_CurrentObject;
  private Vector3 m_LastMousePos;
  private float m_DeltaTime;
  private bool m_AnimateScale;
  private Vector3 m_StartScale;
  private float m_ScaleFactor;
  
  void Update () 
  {
  if(Input.GetMouseButtonDown(0))
  {
  m_LastMousePos = Input.mousePosition;
  m_Ray = Camera.mainCamera.ScreenPointToRay(Input.mousePosition);
  if(Physics.Raycast(m_Ray.origin, m_Ray.direction, out m_RayCastHit, Mathf.Infinity))
  {
  ResizableObject obj = m_RayCastHit.collider.gameObject.GetComponent<ResizableObject>();
  if(obj)
  {
  m_CurrentObject = obj;
  m_StartScale = obj.transform.localScale;
  
  }
  }
  }
  
  if(Input.GetMouseButton(0))
  {
  Vector3 deltaPosition = Input.mousePosition - m_LastMousePos;
  if(deltaPosition.magnitude > 1f)
  {
  if(m_CurrentObject && !m_AnimateScale)
  {
  m_ScaleFactor = deltaPosition.magnitude;
  m_AnimateScale = true;
  m_DeltaTime = 0f;
  }
  }
  m_LastMousePos = Input.mousePosition;
  }
  
  if(m_AnimateScale && m_DeltaTime < 1f)
  {
  m_DeltaTime += Time.deltaTime;
  if(m_CurrentObject)
  {
  m_CurrentObject.transform.localScale = Vector3.Lerp(m_CurrentObject.transform.localScale, m_StartScale * m_ScaleFactor, m_DeltaTime);
  }
  }
  else
  {
  m_AnimateScale = false;
  m_DeltaTime = 0f;
  }
  }
 }

This just scales-up the object not scale-down. It's relatively easy to implement that if you want.

Comment
Add comment · Show 5 · 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 anvi · Aug 15, 2012 at 04:47 PM 0
Share

thank u so much hdsenevi, its really usefull.. i m trying to scale down in the same script but no success.. i hope u'l guide me for that aswell.. thank u..

avatar image anvi · Aug 15, 2012 at 05:05 PM 0
Share

and one more thing, how can i limit that resizable object that it will resize within specific area..

avatar image chabode · Sep 01, 2015 at 04:59 AM 0
Share

how if i tried to combine this codes with movable one? i kinda getting lost from input.getmousebutton(0) because when i tried to change it to touch, it seems can't be implicitly changed to bool

please your help for this one

avatar image Leger DeMain · Mar 19, 2016 at 08:38 AM 0
Share

The controls for this seem a bit wacky for me i copy pasted it so not sure if I,m doing something wrong @hdsenevi

avatar image shaneparsons · Jul 08, 2016 at 04:14 PM 0
Share

Sick indentation.

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

13 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

Related Questions

Drag Object Via Axis Handles 0 Answers

Dragging movement Speed 1 Answer

How To Drag any Cube Using Mouse Click and Moved Only 1 Unit?? 2 Answers

Move Object in its own X - axis to and fro, on mouse drag 1 Answer

dragging camera with mouse results in camera jumping., 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