• 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 tehryan · Oct 20, 2015 at 03:27 AM · localscale

scaling an object based on the distance between it and the mouse cursor

http://i.imgur.com/UPVwMo1.gif

     void Update()
     {
         //if (Input.GetMouseButtonDown(0))
         {
             reach();
         }
 
     }
 
     void reach()
     {
         // find screen pos of tongue
         Vector2 tongue_pos = Camera.main.WorldToViewportPoint(transform.position);
 
         // find screen pos of mouse
         Vector2 mouse_pos = (Vector2)Camera.main.ScreenToViewportPoint(Input.mousePosition);
 
         // calculate angle between points
         float angle = Mathf.Atan2(tongue_pos.y - mouse_pos.y, tongue_pos.x - mouse_pos.x) * Mathf.Rad2Deg;
 
         // calculate the distance between the points
         float distance = Vector2.Distance(tongue_pos, mouse_pos);
 
         // rotate tongue to face mouse
         transform.rotation = Quaternion.Euler(new Vector3(0f, 0f, angle));
         // stretch tongue out to cursor
         transform.localScale = new Vector3(distance*10, transform.localScale.y, transform
 }


I'm trying to stretch this object on it's localscale.x out to the position of the cursor. I thought setting the localScale.x to the distance between the object's pivot and the position of the mouse cursor would do it, but that didn't seem to work. Multiplying that distance by 10 seems to work when the object is scaling vertically but not horizontally. I can't work out what the problem is. Any advice?

Comment
Add comment · Show 3
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 Eno-Khaon · Oct 20, 2015 at 04:29 AM 0
Share

What are the base dimensions of the object you're scaling? (a tongue, I presume?)

If your base shape is an average, 1 unit square, then it's easy. The scale would be equal to the distance you need it to extend, multiplied by the reciprocal of all parents' scale. Then, it would be positioned halfway between its original location and its target.

However, I see no position changes in your script, so is it fair to assume that the origin point on the tongue(?) is not at its center, but potentially on an end?

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Yword · Oct 20, 2015 at 09:48 AM

The unit for the distance should be in World unit, so the tongue_pos and mouse_pos should be in the World space too.

So, the toungue_pos should be modified as:

 // find world pos of tongue
 Vector2 tongue_pos = transform.position;

And the mouse_pos should be modified as:

 // find world pos of mouse
 Vector2 mouse_pos = (Vector2)Camera.main.ScreenToWorldPoint(Input.mousePosition);

Also, don't multiply the distance by 10.

 transform.localScale = new Vector3(distance, transform.localScale.y, transform.localScale.z);
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.

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

31 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

Related Questions

Using transform.localScale to flip object 1 Answer

Scale a prefab without reference to parent 1 Answer

Issue lerping localscale 1 Answer

transform.localScale.x - can't change it 1 Answer

rigid body can rotate but will not move 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