Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
1
Question by JaamiParvez · Mar 01, 2014 at 10:11 PM · mouseobjectsresize

How to create an object and resize it with mouse drag???

Hey Everyone,

I know this question is asked many many so many times ;) .. Searching all day long unfortunatly I still couldn't find what I want..

on left click I want to create an object on mouse position and I want to rescale it only on x axis (for ex: if obj. is created on left click and resize its x axis with on mouse drag )

Hope someone understood what I what :)

(maybe)I have done Half of the work :

     using UnityEngine;
     using System.Collections;
     
     public class Cube : MonoBehaviour
     {
             
             public GameObject CubePrefab;
     
     void Update ()
             {
     if (Input.GetMouseButtonDown (0)) {
     float positionZ = 10.0f;
  Vector3 position = new Vector3 (Input.mousePosition.x, Input.mousePosition.y,positionZ);
     
  Instantiate (CubePrefab, Camera.main.ScreenToWorldPoint (position),transform.rotation);
     
                     }}



this script is attached to main camera with a cubePrefab... this creates an object but now how can I rescale it???

Best.. .J

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

2 Replies

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

Answer by robertbu · Mar 01, 2014 at 10:27 PM

Here is a bit of code that expands on your code. Note that the object growth will not map to the growing size, and note that, since it depends on screen coordinates, you will likely have to do something to recalculate 'sizingFactor' if you target multiple devices.

 using UnityEngine;
 using System.Collections;
 
 public class Cube : MonoBehaviour {
         
     public GameObject cubePrefab;
     public float sizingFactor = 0.02f;
     private GameObject lastSpawn = null;
     private float startSize;
     private float startX;
         
     void Update () {
         if (Input.GetMouseButtonDown (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.GetMouseButton (0)) {
             Vector3 size = lastSpawn.transform.localScale;
             size.x = startSize + (Input.mousePosition.x - startX) * sizingFactor;
             lastSpawn.transform.localScale = size;
         }
     }
 }
Comment
Add comment · Show 3 · 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 JaamiParvez · Mar 02, 2014 at 07:24 AM 1
Share

Thanks @robertbu .. :) thanks a alot ... this solved my problem .. Now the object expands from both side what should I add to expand it only in one side..

Thanks again ...

avatar image adriandevera JaamiParvez · Jan 18, 2016 at 04:08 AM 1
Share

Anyone have a solution for transfor$$anonymous$$g an object from an anchor point (I$$anonymous$$ Top Left) and not the midpoint?

avatar image GafferSystems adriandevera · Jun 01, 2018 at 08:40 PM 1
Share

Just move the object half of the amount added to scale, in the opposite direction.

avatar image
0

Answer by geomorillo · Dec 07, 2016 at 02:26 PM

thanks that formula really helped in a similar case resizing a quad with mouse drag nice size.x = startSize + (Input.mousePosition.x - startX) * sizingFactor;

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

24 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

Related Questions

How do I put a link to a website when I click with the mouse an object or word? 6 Answers

3d Object attach to mouse 1 Answer

How to pick up an object using left mouse button 2 Answers

Select/Drag/Drop Objects with a mouse 0 Answers

Resize / Scale, Drag, Move a Plane at Runtime by Mouse action 2 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