C# 试图在unity3d中将游戏对象保持在屏幕左右边界内
我的游戏对象有以下代码:C# 试图在unity3d中将游戏对象保持在屏幕左右边界内,c#,android,unity3d,C#,Android,Unity3d,我的游戏对象有以下代码: private float screenx; Vector3 playerPosScreen; void Start () { screenx=Camera.main.pixelWidth-renderer.bounds.size.x ; } void update(){ playerPosScreen = Camera.main.WorldToScreenPoint(transform.position); if (playerPosScreen.x
private float screenx;
Vector3 playerPosScreen;
void Start () {
screenx=Camera.main.pixelWidth-renderer.bounds.size.x ;
}
void update(){
playerPosScreen = Camera.main.WorldToScreenPoint(transform.position);
if (playerPosScreen.x >= screenx) {
//playerPosScreen.x=screenx;
transform.position=new Vector3 (screenx, transform.position.y,transform.position.z);
}
//txt.text = playerPosScreen.x.ToString();
else if(playerPosScreen.x<=renderer.bounds.size.x){
transform.position=new Vector3 (renderer.bounds.size.x, transform.position.y,transform.position.z);
}
}
我正在开发一个带有正交摄像头的2D游戏,我的问题是游戏对象一直在屏幕外,我是不是遗漏了什么?首先,你的函数从来没有被调用过,因为它的名字有拼写错误。它应该是更新的,而不是更新的 其次,坐标的问题是代码混合了屏幕坐标和世界坐标。屏幕坐标从0,0到Screen.width,Screen.height。可以使用WorldToScreenPoint将坐标从世界坐标更改为屏幕坐标,然后使用ScreenToWorldPoint将坐标更改为屏幕坐标,其中Z值是转换点与摄影机的距离 下面是一个完整的代码示例,可在更改播放器位置后使用,以确保其位于屏幕区域内:
Vector2 playerPosScreen = Camera.main.WorldToScreenPoint(transform.position);
if (playerPosScreen.x > Screen.width)
{
transform.position =
Camera.main.ScreenToWorldPoint(
new Vector3(Screen.width,
playerPosScreen.y,
transform.position.z - Camera.main.transform.position.z));
}
else if (playerPosScreen.x < 0.0f)
{
transform.position =
Camera.main.ScreenToWorldPoint(
new Vector3(0.0f,
playerPosScreen.y,
transform.position.z - Camera.main.transform.position.z));
}
首先,你的函数永远不会被调用,因为它的名字有拼写错误。它应该是更新的,而不是更新的 其次,坐标的问题是代码混合了屏幕坐标和世界坐标。屏幕坐标从0,0到Screen.width,Screen.height。可以使用WorldToScreenPoint将坐标从世界坐标更改为屏幕坐标,然后使用ScreenToWorldPoint将坐标更改为屏幕坐标,其中Z值是转换点与摄影机的距离 下面是一个完整的代码示例,可在更改播放器位置后使用,以确保其位于屏幕区域内:
Vector2 playerPosScreen = Camera.main.WorldToScreenPoint(transform.position);
if (playerPosScreen.x > Screen.width)
{
transform.position =
Camera.main.ScreenToWorldPoint(
new Vector3(Screen.width,
playerPosScreen.y,
transform.position.z - Camera.main.transform.position.z));
}
else if (playerPosScreen.x < 0.0f)
{
transform.position =
Camera.main.ScreenToWorldPoint(
new Vector3(0.0f,
playerPosScreen.y,
transform.position.z - Camera.main.transform.position.z));
}
经过一些研究,我发现一个解决方案非常适合我:
Vector3 playerPosScreen;
Vector3 wrld;
float half_szX;
float half_szY;
void Start () {
wrld = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, 0.0f, 0.0f));
half_szX = renderer.bounds.size.x / 2;
half_szY = renderer.bounds.size.y /2 ;
}
void Update () {
playerPosScreen = transform.position;
if (playerPosScreen.x >=(wrld.x-half_szX) ) {
playerPosScreen.x=wrld.x-half_szX;
transform.position=playerPosScreen;
}
if(playerPosScreen.x<=-(wrld.x-half_szX)){
playerPosScreen.x=-(wrld.x-half_szX);
transform.position=playerPosScreen;
}
}
经过一些研究,我发现一个解决方案非常适合我:
Vector3 playerPosScreen;
Vector3 wrld;
float half_szX;
float half_szY;
void Start () {
wrld = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, 0.0f, 0.0f));
half_szX = renderer.bounds.size.x / 2;
half_szY = renderer.bounds.size.y /2 ;
}
void Update () {
playerPosScreen = transform.position;
if (playerPosScreen.x >=(wrld.x-half_szX) ) {
playerPosScreen.x=wrld.x-half_szX;
transform.position=playerPosScreen;
}
if(playerPosScreen.x<=-(wrld.x-half_szX)){
playerPosScreen.x=-(wrld.x-half_szX);
transform.position=playerPosScreen;
}
}
我为输入错误感到抱歉,这只是一个输入错误,感谢您提供有关说明和代码的提示。我将测试它:没问题。至少对我来说,这段代码适用于正字法和透视相机。我很抱歉键入错误更新,这只是一个键入错误,感谢您提供有关说明和代码的提示。我将测试它:没问题。至少对我来说,这段代码对正交和透视相机都有效。