- Home /
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?
Your answer
Follow this Question
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