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
		}

1 Answer

1

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: Vectrosity line with collider? - Questions & Answers - Unity Discussions

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.

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. Maybe 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. http://imgur.com/hwBoC2X

public void UpdateLine(Transform linePrefab, Vector3 from, Vector3 to, float lineWidth, Vector3 toTarget) { Vector3 distance= to - from; transform.position = from + (distance/ 2f); transform.localScale = new Vector3(distance.magnitude/5.43f, lineWidth, 1f); } I've got it with this code. Why do you think that dividing by 5 would make it align nearly perfectly and work? I didn't use transform.right = toTarget.normalized; because it kept causing the line to rotate and throwing off the 2d collider.