简体   繁体   中英

Unity clamping issues

I am trying to only allow the pong paddle in my pong game to go to the top of the screen and bottom then stop. I figured I would set the yClamp variable to be the camera size - the height of the texture divided by 2. However this doesn't work at all. The top should be approximately, with my aspect ratio I am working with, between 3.2 and 3. Here is the code I am trying to accomplish this with:

    renderer = GetComponent<SpriteRenderer>();

    yClamp = Mathf.Abs(Camera.main.orthographicSize - 
        ((renderer.sprite.textureRect.height / renderer.sprite.pixelsPerUnit)/2f));

However this only allows me to go to 1.43 and -1.43. I can't seem to figure out why this won't work. All the logic is there. The paddle starts at (0,0) so I don't see where my logic is wrong.

Any help with this is extremely appreciated!

Note: Just to clear things up, the clamp should be between, roughly, -3.2 and 3.2 However, I need a formula for this because I will be dealing with many resolutions. This is my debug state.

I don't see why it wouldn't be working.

Here is what I would check

  • You assume to be placed in y = 0 coordinate. Is it correct ?
  • Camera orthoSize is half the view size (but I think you use it correctly)
  • does renderer.sprite.textureRect.height give the proper pixel result?
  • What if you use Sprite.bounds instead?

You only have few variables, it shouldn't be difficult to figure which one doesn't contain the expected value. If this doesn't work, could you post the value of each variable (height, camera size, pixel per unit)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM