简体   繁体   English

Unity夹紧问题

[英]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. 我以为可以将yClamp变量设置为相机大小-纹理的高度除以2。但是,这根本不起作用。 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: 以我正在使用的长宽比,顶部应该大约在3.2到3之间。这是我尝试使用的代码:

    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. 但是,这只允许我转到1.43和-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. 桨开始于(0,0),所以我看不出我的逻辑错在哪里。

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. 注意:只是为了清理起见,钳位应该大致介于-3.2和3.2之间。但是,我需要一个公式,因为我将处理许多分辨率。 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. 您假设将其放置在y = 0坐标中。 Is it correct ? 这是正确的吗 ?
  • Camera orthoSize is half the view size (but I think you use it correctly) Camera orthoSize是视图大小的一半(但我认为您使用正确)
  • does renderer.sprite.textureRect.height give the proper pixel result? renderer.sprite.textureRect.height给出正确的像素结果?
  • What if you use Sprite.bounds instead? 如果改用Sprite.bounds怎么办?

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) 如果这样做不起作用,您能否发布每个变量的值(高度,相机大小,单位像素)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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