Getting size of laser to offset position when fired

I am using:

laser.GetComponent<SpriteRenderer>().size.y / 2

to determine how much to offset the laser when instantiating it. When I measure the units by manually moving it up half its height I get 0.266 units, but when I print out this value computed above, I get 0.165. Is there any obvious reason why my thinking that the spriterenderer size is the way to find the image’s full height is wrong?

Hi,

SpriteRenderer.size is used when you the SpriteRenderer.drawMode set to NineSlice.

The sprite will be stretched from the centre, horizontally and vertically, to fit the sprite’s size.

You could consider using the rect transform’s height property instead, although just dividing this by 2 may not be sufficient.

I am assuming that you have the transforms at the centre of both the ship and the lasers, assuming so, you would probably want to calculate half the height of the ship and half the height of the laser, and then add them together to be used as the offset. That way, the laser should appear just in front of the ship. Of course, it depends on what you are trying to achieve, but this is fairly common as an approach.


See also;

Thank you, Rob. I did in fact algorithmically use the approach of half the ship and half the laser (with each zero centered). I just simplified the question above because the results I was seeing just for the laser didn’t match up. The drawmode on my sprite appears to be “Simple” which I assume is default? So, are you saying that when not set to Slice, this spriteRenderer.size is not reflective of the sprite’s bounds size?

I will try the transform property but it seems visually to not have sizing info but just pos rot scale. I just was wondering if there was something to query that was reflective of the size after any applied transforms (scale) or other things affected the sprite.

Hi,

The drawmode on my sprite appears to be “Simple” which I assume is default?

That’s correct, yes.

So, are you saying that when not set to Slice, this spriteRenderer.size is not reflective of the sprite’s bounds size?

Yes, and no :slight_smile:

If you take a look at that link above, you’ll note that only the middle part of the sprite is affected by the draw mode being set to NineSlice, my understanding is that the central part of that then uses the size, so, section E in the diagram.

I just was wondering if there was something to query that was reflective of the size after any applied transforms (scale) or other things affected the sprite.

You could have a look at the bounds property of the SpriteRenderer as well.


See also;

SpriteRenderer.bounds.extents.y worked perfectly to find the half-size. Thank you for that. It’s still a mystery why using the SpriteRenderer.size gives me .37 y when the image is 54 pixels high (100 px per unit so I’d have expected .54 y which completely agrees with the bounds.extents.y = .27. I’m glad this bounds property does work though!

1 Like

No problem, glad you have what you need.

I would need to experiment a little with it, but my feeling is that the .37 would represent the red part of the image below;

NineSlice

With the green areas representing the .17, split between them.

Again, would need to experiment a little to be sure, and probably with an image of equal proportions and then set to an easy size, like double, and then output the various properties to the console.

Of course, it could just be utterly random and not represent anything significant until used with the correct draw mode :slight_smile:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.