Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 LegendaryZReborn · May 02, 2016 at 05:18 AM · unity 52d gameunity 2dlocalscalelaser beam

LocalScale doesn't scale sprite to the distance specified

I'm using Unity 5.3.4 for starters. Im trying to implement an extendable lazer but Im running into some problems. I followed this tutorial:

http://steredenn-game.tumblr.com/post/98397504410/steredenn-making-an-expandable-laser

The code I'm using:

if (start == null) { start = (GameObject)Instantiate (rstart); start.transform.parent = this.transform; start.transform.localPosition = Vector2.zero; }

     if (middle == null) 
     {
         middle = (GameObject)Instantiate (rmiddle);
         middle.transform.parent = this.transform;
         middle.transform.localPosition = Vector2.zero;
     }

     currentRailgunSize = maxRailgunSize;
     rDirection = this.transform.right;
     RaycastHit2D hit = Physics2D.Raycast (this.transform.position, rDirection, maxRailgunSize);

     if (hit.collider != null) 
     {
         //we hit something
         currentRailgunSize = Vector2.Distance(hit.point, this.transform.position);

         // -- Create the end sprite
         if (end == null)  
         {
             end = Instantiate(rend) as GameObject;
             end.transform.parent = this.transform;
             end.transform.localPosition = Vector2.zero;
         }
     }
     else
     {
         // Nothing hit
         // -- No more end
         if (end != null) {
             Destroy (end);
         }
     }

     // Place things
     // -- Gather some data
     float startSpriteWidth = start.GetComponent<Renderer>().bounds.size.x;
     float endSpriteWidth = 0f;

     if (end != null) {
         endSpriteWidth = end.GetComponent<Renderer> ().bounds.size.x;  
     }
     // -- the middle is after start and, as it has a center pivot, have a size of half the laser (minus start and end)
     middle.transform.localScale = new Vector3(currentRailgunSize- startSpriteWidth , middle.transform.localScale.y, middle.transform.localScale.z);
     middle.transform.localPosition = new Vector2((currentRailgunSize) /2f, 0f);


     // End? 
     if (end != null)   
     {
         end.transform.localPosition = new Vector2(currentRailgunSize, 0f);    

     }  
      
 } 

However, middle sprite of the laser seems to be scaling slower in comparison to the distance between the hit point and the starting position(local's parent). I even tried this method to get the desired length of the middle sprite:

http://answers.unity3d.com/questions/432009/resizing-a-gameobject-based-on-mouse-position.html

Code modified:

// Place things // -- Gather some data

     float startSpriteWidth = start.GetComponent<Renderer>().bounds.size.x;
     rendererz = middle.GetComponent<Renderer> ();
     float middleSpriteWidth = rendererz.bounds.size.x;
     float endSpriteWidth = 0f;

     if (end != null) {
         endSpriteWidth = end.GetComponent<Renderer> ().bounds.size.x;  
     }
     // -- the middle is after start and, as it has a center pivot, have a size of half the laser (minus start and end)
     middle.transform.localScale = new Vector3(currentRailgunSize/middleSpriteWidth , middle.transform.localScale.y, middle.transform.localScale.z);
     middle.transform.localPosition = new Vector2((currentRailgunSize) /2f, 0f);


     // End? 
     if (end != null)   
     {
         end.transform.localPosition = new Vector2(currentRailgunSize, 0f);    

     }  

but I got this weird effect where the beam would spaz out and return to a shorter length.

So right now I'm looking for any suggestions I can possible get. The tutorial did mention the middle sprite has to be 1 pixel per unit (mine is 100) but when i changed it to 1 pixel per unit it was way too big for the map so I change it back to 100 pixels per unit.

Any thoughts?

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

0 Replies

· Add your reply
  • Sort: 

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

OnCollisionEnter2D dont work,OnCollisionEnter2D dont work 2 Answers

How to create a 2d game grid background for my game? 0 Answers

how to resize different sprite into same size and assign to a game object ? 0 Answers

Making the player change his movement when it hit an object with collider 0 Answers

How can i save a variable for only one scene? 1 Answer


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