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
0
Question by psk.psk6 · Dec 01, 2014 at 11:11 PM · sprite

Unity Stretch Sprite between two Points at runtime

How to stretch a Sprite between two points say initialPosition , FinalPosition in world coordinates, Please elaborate with code, Thanks

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
7

Answer by jenci1990 · Dec 01, 2014 at 11:36 PM

 using UnityEngine;
 using System.Collections;
 
 public class Player : MonoBehaviour {
     public Vector3 startPosition = new Vector3(0f,0f,0f);
     public Vector3 endPosition = new Vector3(5f, 1f, 0f);
     public bool mirrorZ = true;
     void Start() {
         Strech(gameObject, startPosition, endPosition, mirrorZ);
     }
 
 
     public void Strech(GameObject _sprite,Vector3 _initialPosition, Vector3 _finalPosition, bool _mirrorZ) {
         Vector3 centerPos = (_initialPosition + _finalPosition) / 2f;
         _sprite.transform.position = centerPos;
         Vector3 direction = _finalPosition - _initialPosition;
         direction = Vector3.Normalize(direction);
         _sprite.transform.right = direction;
         if (_mirrorZ) _sprite.transform.right *= -1f;
         Vector3 scale = new Vector3(1,1,1);
         scale.x = Vector3.Distance(_initialPosition, _finalPosition);
         _sprite.transform.localScale = scale;
     }
 }
Comment
Add comment · Show 4 · 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 krav_tzar · Dec 02, 2015 at 08:12 AM 0
Share

Excellent function!

Thanks!

avatar image FoxProject · Feb 18, 2017 at 08:53 PM 0
Share

That worked for med too, though I had to multiply scale.x with 100. Thanks

avatar image BenChedli · Aug 25, 2017 at 08:36 AM 0
Share

Worked for me too, i only had to divide scale.y by 100, thank you very mush.

avatar image Hozei · Nov 29, 2018 at 09:14 PM 0
Share

Had to take in consideration the length of the original image and divide by the scale for it to work for me:

 float width = obj.GetComponent().bounds.size.x;
         Vector3 centerPos = (initialPosition + finalPosition) / 2f;
         obj.transform.position = centerPos;
         Vector3 direction = finalPosition - initialPosition;
         direction = Vector3.Normalize(direction);
         obj.transform.right = direction;
         if (mirrorZ) obj.transform.right *= -1f;
         Vector3 scale = new Vector3(1, 1, 1);
         scale.x = Vector3.Distance(initialPosition, finalPosition)/ width;
         obj.transform.localScale = scale;
 
avatar image
1

Answer by BlackManta · Dec 02, 2014 at 12:23 AM

It would be difficult to do it from the sprite, From what I have used with sprites you could change the scale, rotation and position and such to make it fit between two points but using to points to find that would be a little trickier. (I suppose if you wanted to do the math you could derive your own rotation and scales from the two points.)

If a GUI is an option you could use this:

 GUI.DrawTexture (new Rect (<X1>, <Y2>, <X1>-<X2>, <Y1>-<y2>), MySprite.texture, ScaleMode.ScaleToFit);


Hope that helps. Cheers, BM

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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How can I get the height of a sprite? 1 Answer

2D character face another character 2 Answers

How to change the textureRect of a Sprite with a dynamic way? 0 Answers

how to decrease vertices number of a sprite 0 Answers

Create a Sprite Animation in Code 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