• 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
1
Question by BiffJenkins · Jan 07, 2016 at 11:12 AM · c#instantiatebuttonmousedrag

Instantiate and Scale with Mouse Click.

Hello Everyone, I have searched around and haven't been able to find quite what I am looking for. I understand how to instantiate an object where the mouse is clicked. However, I would like to implement more functionality.

  1. Begin "drawing" object when left mouse button down. This would also act as an anchor/pivot point while the mouse is dragged around the screen.

  2. Complete "drawing" object when left mouse button is released.

The object would scale only in its y while maintaining scale in both x and z. Any help would be greatly appreciated and I apologize if this has been answered elsewhere. Also, I would be writing this in C#

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
1
Best Answer

Answer by RyFridge · Jan 07, 2016 at 02:43 PM

Hey @BiffJenkins, I would use Input.mousePosition and Camera.ScreenPointToRay(). Try this script I just wrote and drop it on one of your Cameras:

     [SerializeField] GameObject prefab;
     [SerializeField] [Range(1f, 200f)] float drawDistance = 50f;
 
     GameObject drawObject;
     bool drawing = false;
     Vector3 mouseStartingPosition;
 
     Camera cam;
 
     void Start(){
         // load our camera
         cam = GetComponent<Camera> ();
     }
     
     // Update is called once per frame
     void Update () {
         // check if we want to start, end, or continue drawing
         if (Input.GetMouseButtonDown (0)) {
             startDraw ();
         } else if (Input.GetMouseButtonUp (0)) {
             endDraw ();
         } else if (drawing) {
             whileDrawing ();
         }
     }
 
     void startDraw(){
         // create a new instance
         drawObject = Instantiate (prefab, Vector3.zero, Quaternion.identity) as GameObject;
 
         // save our draw starting point
         mouseStartingPosition = Input.mousePosition;
 
         // put the new instance where we pointed to with a set distance (or collision)
         Ray ray = cam.ScreenPointToRay(mouseStartingPosition);
         drawObject.transform.position = transform.position + ray.GetPoint (drawDistance);
 
         // allow the Update to call whileDrawing()
         drawing = true;
     }
 
     void endDraw(){
         // forbid the Update do call whileDrawing()
         drawing = false;
     }
 
     void whileDrawing(){
         // manipulate the instance in whatever way you like
         float mouseDistance = Vector3.Distance (mouseStartingPosition, Input.mousePosition);
         drawObject.transform.localScale = new Vector3 (1f, mouseDistance, 1f);
     }
Comment
Add comment · Show 1 · 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 BiffJenkins · Jan 14, 2016 at 04:42 AM 0
Share

Thank you very much, this was incredibly helpful.

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to check for a mouse drag 0 Answers

How to Instantiate from correct Icon. 1 Answer

How to disable button OnClick when drag on the screen with OnMouseDrag 2 Answers

dragging camera with mouse results in camera jumping., 0 Answers

Controlling an animation by dragging a gameObject with the mouse 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