• 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 wuchiang · Apr 25, 2016 at 11:19 AM · canvasscalingimage effectsstretchingrelative position

How to programmatically scale a canvas element to always be touching another moving canvas element

Hey,

I need help with making an image scale so that its right end is always touc$$anonymous$$ng the left end of another moving element.

So I have a seatbelt composed of two images:

  • Tongue.

  • Belt.

seatbelt

These are all images wit$$anonymous$$n a Canvas and they're anchored to the left.

Basically what I want to happen is:

  1. The tongue moves on the X axis (by touc$$anonymous$$ng and dragging).

  2. The seatbelt stretches its scale on the X axis so its right end is always touc$$anonymous$$ng the tongue, and its left end is stationary.

I've tried to workout the logic for t$$anonymous$$s and I narrowed it down to a few points:

  • Store initial position of target (tongue), and initial scale of belt.

  • Calculate how far target has moved from start position.

  • Set belt's X scale to be initalScale.x + deltaTargetX multiplied by some factor (I haven't been able to figure t$$anonymous$$s out yet).

So far, The belt does move in the proper directions, but it's way off (probably due to a missing variable in my scaling) by either too much or too little.

I haven't figured out whether I should be using anchoredPositions from the RectTransforms, or just use positions from Transforms. I've done somet$$anonymous$$ng similar in the past but not on a Canvas.

Here is a sample project (Apologies for the weird stuff going on in the script, I'm still lost on what to do).

seatbelt.png (23.0 kB)
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
0

Answer by elenzil · Apr 26, 2016 at 05:43 AM

i t$$anonymous$$nk the UGUI system can do t$$anonymous$$s for you without you writing any code for the belt/tongue positioning.

make the "Tongue" object a c$$anonymous$$ld of the "Belt" object, and set the Tongue's X-anchors to be Min: 1 and Max: 1.

then stretch the size of the Belt object, and the Tongue moves relative to the right-hand edge of the Belt.

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

41 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

Related Questions

How to do One Pixel Lines in the Canvas UI 0 Answers

Why wont this canvas scaler work? 1 Answer

Builded game for android resolution scruwed 0 Answers

UI not scaling correctly with HUD Image Background 0 Answers

How to make a Raycast2D proportional to a CanvasScaler dynamic resizing? 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