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 /
avatar image
0
Question by nj4242 · Nov 25, 2016 at 12:15 PM · 2druntimescalingdragginggizmo

Creating a custom runtime scaler !?

Hi Unity Enthusiasts,

I am creating a scaler function which will let me scale objects at runtime. Basically like the transform gizmo in the Scene View. I do have a dragging script which will allow me to move my objects in screen world directions. I don't want to use the UI scaling an all, I am only concerned with sprites and objects to scale with.

Now, I know we could probably achieve a runtime scaling just by using transform.localScale.x + 5f; from a button click or event system. But I want to do with a simpler edge collider or something I don't know upon my 2D square gameobject (so as to scale square into rectangle of various sizes) by drag object to scale in either directions x or y.

This is my dragging script:

 var screenSpace;
  var offset;
   
  function OnMouseDown(){
      //translate the cubes position from the world to Screen Point
      screenSpace = Camera.main.WorldToScreenPoint(transform.position);
       
      //calculate any difference between the cubes world position and the mouses Screen position converted to a world point  
      offset = transform.position - Camera.main.ScreenToWorldPoint(Vector3(Input.mousePosition.x,Input.mousePosition.y, screenSpace.z));
     
  }
   
  /*
  OnMouseDrag is called when the user has clicked on a GUIElement or Collider and is still holding down the mouse.
  OnMouseDrag is called every frame while the mouse is down.
  */
   
  function OnMouseDrag () {
   
      //keep track of the mouse position
      var curScreenSpace = Vector3(Input.mousePosition.x, Input.mousePosition.y, screenSpace.z);    
   
      //convert the screen mouse position to world point and adjust with offset
      var curPosition = Camera.main.ScreenToWorldPoint(curScreenSpace) + offset;
   
      //update the position of the object in the world
      transform.position = curPosition;
  }

The problem is that I don't know how to implement scaling function at runtime inside the dragging script, I however tried the scaling script for 3D object scales objects from the center pivot only and does not allow scale down and but also scale up in all directions, which I pretty much I never wanted. Here is a 3D object scaler script if anybody want a reference ( its in the comments below).

P.S : I did saw some of the giant level editor in asset store which has everything of unity gizmo has to offer. I really don't want much that of it, I might not able to implement it as just a simple scaling, even If I'd managed to buy from those of only available expensive ones.

  • P.S : To let you guys know, I don't know If it would be helpful enough that I'm scaling a 2D sprite [not a 3d object], from a localPositionX and localPostionY thats it, while we drag the edges of the 2d object (basically I have a white 2d square which I want to scale).

It would be very thankful if someone help me achieve this. :)) Regards.

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 nj4242 · Nov 25, 2016 at 12:19 PM 0
Share

3d working Scaler script (w/o gizmo) which I tried (unuseful):

 using UnityEngine;
  using System.Collections;
  
  public class ScaleObject : $$anonymous$$onoBehaviour {
          
      public GameObject cubePrefab;
      public float sizingFactor = 0.02f;
      private GameObject lastSpawn = null;
      private float startSize;
      private float startX;
          
      void Update () {
          if (Input.Get$$anonymous$$ouseButtonDown (0)) {
              float positionZ = 10.0f;
              Vector3 position = new Vector3 (Input.mousePosition.x, Input.mousePosition.y,positionZ);
              startX = position.x;
              position = Camera.main.ScreenToWorldPoint(position);
              lastSpawn = Instantiate (cubePrefab, position,transform.rotation) as GameObject;
              startSize = lastSpawn.transform.localScale.z;
          }
  
          if (Input.Get$$anonymous$$ouseButton (0)) {
              Vector3 size = lastSpawn.transform.localScale;
              size.x = startSize + (Input.mousePosition.x - startX) * sizingFactor;
              lastSpawn.transform.localScale = size;
          }
      }
  }
avatar image nj4242 · Nov 25, 2016 at 12:25 PM 0
Share

Also the dragging script in the post is in javascript and I'm working in C#, I have referenced it from here: http://forum.unity3d.com/threads/drag-drop-game-objects-without-rigidbody-with-the-mouse.64169/

0 Replies

· Add your reply
  • Sort: 

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

95 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

Related Questions

Problem in adaptation code vector3 for vector2 0 Answers

Colide objects with runtime gizmo movement 0 Answers

Create Sprite at Runtime 1 Answer

Change pixels per unit for an imported asseet, but only on x axis 1 Answer

RectTool scale in both direction,RectTool scale in both directions 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