x


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.

more ▼

asked Aug 14, 2012 at 08:48 PM

anvi gravatar image

anvi
3 5 6 7

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

Aug 14, 2012 at 10:10 PM flaviusxvii

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.

Aug 15, 2012 at 12:35 AM Loius
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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.

more ▼

answered Aug 15, 2012 at 09:58 AM

hdsenevi gravatar image

hdsenevi
399 3 7 13

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..

Aug 15, 2012 at 04:47 PM anvi

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

Aug 15, 2012 at 05:05 PM anvi
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1479
x502
x175

asked: Aug 14, 2012 at 08:48 PM

Seen: 6084 times

Last Updated: Aug 15, 2012 at 05:05 PM