- Home /
How to make sprite renderer increase in size on only one side of x axis?
I have the following code to make a sprite rotate around a character in 2D towards the mouse pointer, however, I'd like to know how to increase the sprite renderer size so that it stretches from the player to the mouse pointer. At present, when I increase size, it obviously increases the size on both ends of the Sprite Renderer. I'd like to only increase the size on one side. I think the only way to do this is to move the sprite's position relative to the size increase and parent object, which I'm having difficult time with.
Also, the reason I'm not using LineRenderer is becasue I have a 2D collider on the sprite, otherwise I would have and the problem would have been solved hours ago.
float angle;
Vector3 mousePos = Input.mousePosition;
Vector3 objectPos = Camera.main.WorldToScreenPoint(this.transform.position);
mousePos.x = mousePos.x - objectPos.x;
mousePos.y = mousePos.y - objectPos.y;
angle = Mathf.Atan2(mousePos.y, mousePos.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle));
if(this.transform.tag == "Laser")
{
mousePos = Input.mousePosition;
Vector3 worldPos = worldCamera.ScreenToWorldPoint(mousePos);
float x = worldPos.x;
float y = worldPos.y;
float z = worldPos.z;
float beamx = this.transform.position.x;//* 15.0f;
float beamy = this.transform.position.y;// * 15.0f;
float beamz = this.transform.position.z;// * 15.0f;
float distance = Vector3.Distance(new Vector3(x, y, 1), new Vector3(beamx, beamy, 1));
print (beamx+" "+beamy+" "+beamz + " Distance: "+distance+ " "+this.transform.localPosition+" "+worldPos);
transform.localScale = new Vector3(distance/transform.parent.transform.localScale.x, 1.0f/transform.parent.transform.localScale.y, 1.0f/transform.parent.transform.localScale.z);
this.transform.position = transform.parent.position;
//insert code to reposition the line so it looks like it is only increasing in size only on one end
}
Answer by Tomer-Barkan · Nov 25, 2013 at 04:36 PM
You can change the localScale vector. Changing the X will change the size of the sprite in the X plane:
transform.localScale += new Vector3(1, 0, 0); // increase the X scale by 1.
For drawing lines from point A to point B, you might want to look at my answer here: http://answers.unity3d.com/questions/579604/vectrosity-line-with-collider.html
It uses 3d cubes and not sprites, but the idea is the same.
Edit: Here is a modified script that should work when the arrow is pointing right instead of up, and with a sprite instead of cube:
public void DrawLine(Transform linePrefab, Vector3 from, Vector3 to, float lineWidth) {
framesWithoutFiring = 0;
// calculate vector from point A to point B
Vector3 lineVector = to - from;
// instantiate line
Transform lineInstance = GameObject.Instantiate(linePrefab);
// set position of line instance to center of line
lineInstance.position = from + (lineVector / 2f);
// set rotation of line instance so that it's Y axis is from point A to point B
lineInstance.transform.right = toTarget.normalized;
// set length of line instance
lineInstance.transform.localScale = new Vector3(lineVector.magnitude, lineWidth, lineInstance.transform.localScale.z);
}
Now when you use it, simply provide the parent's position in the "from", and the mouse's position in the to (don't forget to translate mouse screen position to world position with camera.main.ScreenToWorldPosition)
Perhaps my question wasn't clear, but if I increase the size by X it extends the line on both left and right side. I want the line to appear as if it is only getting bigger on one side while at the same time maintaining its position pointed towards the mouse cursor.
Yes, the object's position is always the center of the line/sprite... What you have to do is change the scale, and then change the position accordingly. So if the scale grows by 1, the position grows by 1/2, that way the leftmost part will remain the same (you increased the size by 1/2 in each side, but you also moved it to the right by 1/2, so in total you increased by 1 to the right only).
I use this exact trick in the link I put in my answer.
If it is important for you that the position does not change, you could create a parent empty object whose position will not change, and make the sprite a child object, whose position is allowed to change but still keeping the original position in the parent.
I see what you mean, thanks for responding, I guess I'll just have to keep playing around with it until I get it and try your suggestions. I have this object attached to a parent object. The transform always needs to begin at the player and extend to whereever the mouse pointer moves on the screen in 2D space. I've tried your suggestion but with transform.right ins$$anonymous$$d of up because this line only increases in size on the x axis, not y or z. Either way its still not working right, unfortunately the line still shoots out both ways when I just want it to appear as if it is getting longer in one direction from the player to the mouse cursor.
Updated the answer with a script that should work with X scale ins$$anonymous$$d of Y (when the arrow is pointing right). You had to change the last command too, passing the correct size to the X variable ins$$anonymous$$d of the Y.
Thanks, I've done as you suggested but here are the problems that occur still. It is just odd behaviour, I cant tell if its simply because Unity 4.3 2D is causing problems or not. $$anonymous$$aybe it's also because I haven't slept in 30 hours. Here's a pic of the issue. You can see now the line draws to the mouse, the mid way point on the transform is half way between the character and mouse, but the other end of the line just goes off into space oddly.
Your answer
Follow this Question
Related Questions
Unity streching sprite gameobject to fit two positions. 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Scrolling sprite image 0 Answers
How to get the spriterender default material in the code 1 Answer