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 /
avatar image
1
Question by Rytif · Apr 05, 2013 at 03:51 AM · gameobjectmousescaling

Resizing a gameobject based on mouse position

Hello, I'm trying to make a 2D game where the player builds bridges. These bridges are made by clicking and dragging from one connection point to another, while doing so the bridge is being drawn and following the mouse. The bridge is also on a pivot on its first connection, so it should rotate in the direction of the mouse and only extend its width on the opposite side.

Here's a demonstration:

alt text

I'm having difficulty getting the gameobject (bridge) to increase its width to keep up with the position of the mouse. For instance, I've tried increasing the scale of the bridge but, the scale extends it in both directions (instead of just the side I'm dragging with the mouse). Would it be best to edit the far right vertices and set their position to the mouse?

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

Answer by Fattie · Apr 05, 2013 at 03:54 AM

don't edit any vertices. Just adjust the localScale of the object along the X axis.

it's that simple. have two markers (empty game objects) at 1st connection and 2nd connection

Get the distance between them using ".distance"

Say it is 4.3 meters. Say the natural length of your blue object is 2.7 meters.

Simply set the scale on X of the blue object to (4.3/2.7)

if you have trouble using .distance or scale, simply search on here for 100s of answers about them, or ask a separate question

http://docs.unity3d.com/Documentation//ScriptReference/Transform-localScale.html

http://docs.unity3d.com/Documentation//ScriptReference/Vector3.Distance.html


As I mentioned below typically you let the software get the original length. Fortunately this is very easy in unity. I opened the first project I saw in front of me and searched on "size" and found dozens of examples!

 var lengthNow:float;
 lengthNow = ourSprite.renderer.bounds.size.x;
 
 transform.localScale.x = ( desiredLength / lengthNow );
 transform.localPosition.x += 0.5 * desiredLength;

Note that it's also moving it so the center stays in the correct position!

one point -- the incredibly useful RotateAround function is often useful with this. Cheers

Comment
Add comment · Show 5 · 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 Rytif · Apr 06, 2013 at 04:27 AM 0
Share

Thanks, that works!

avatar image Fattie · Apr 06, 2013 at 06:15 AM 0
Share

great! it's a "standard trick"

here's something that may help. as you know in program$$anonymous$$g you try to generalize everything, to make it easier next time.

You see where I said "the original length is 27 meters"

in fact. With one line of code, your software can FIND the length of the thing. Rather than you having to measure it and hard-code it in.

what you usually do is simply use the renderer, and get the size of it! it's great - one line of code. Indeed you see "renderer.bounds. ..." everywhere in video game code. I'll post in an example.

avatar image lemiwinks · Sep 23, 2013 at 12:52 PM 0
Share

ive seen this but it doesnt work for me, im using this line

ArrowPrefab.transform.localScale.x = dragDistance;

but im getting the same error

Cannot modify a value type return value of `UnityEngine.Transform.localScale'. Consider storing the value in a temporary variable

but im pretty sure dragDistance is already a variable or am i wrong?

this is the full script http://hostcode.sourceforge.net/view/1023

avatar image Fattie · Jan 14, 2014 at 07:58 AM 0
Share

lem, are you using c# or U/S ? if c#, you just need to set the whole vector at the same time. see 100000s of examples on here

avatar image MrVerdoux · Jan 14, 2014 at 08:10 AM 0
Share

@lemiwinks, I have seen that error a million times. If I´m not wrong, you can´t assign the x of a localScale vector directly, you have to assign a whole vector.

Ins$$anonymous$$d of: ArrowPrefab.transform.localScale.x = dist; use this: Vector3 temp = ArrowPrefab.transform.localScale; temp.x = dist; ArrowPrefab.transform.localScale = temp;

There are also other vectors which you have to change this way (storing data in a vector) but I don´t remember them right now. Anytime you see the error you described, this is likely to be the cause.

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

13 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

Related Questions

move Game Object(hand) with mouse movement 2 Answers

Game Object flies out of screen when dragged 1 Answer

How can i Select a GameObject (Player) and be able to move that from point A to point B with the mouse? 1 Answer

Detecting when a game object is clicked, when the mouse is held down, or enters or leaves a GameObject 2 Answers

Raycast moved object won't go under certain height 3 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