【发布时间】:2014-11-15 12:44:12
【问题描述】:
我创建了一个简单的拖动脚本。这将是我游戏的主要输入,也是我控制玩家的方式。脚本如下:
public void OnMouseDown() {
offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint (new Vector3 (Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
if (gameController.GetCurrentState () != GameStates.INGAME){
gameController.StartGame();
}
}
public void OnMouseDrag() {
if (gameController.GetCurrentState () == GameStates.INGAME) {
Vector3 curScreenPoint = new Vector3 (Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
Vector3 curPosition = Camera.main.ScreenToWorldPoint (curScreenPoint) + offset;
transform.position = curPosition;
}
}
这个脚本附在我的播放器上。我的播放器还附加了一个 RigidBody 2D 和 Circle Collider 2D。我已经创建了一些墙,在游戏开始时,我正在使用 mainCam.ScreenToWorldPoint 将其重新定位到摄像机之外。这是通过以下方式完成的:
var mainCam : Camera;
var topWall : BoxCollider2D;
var bottomWall : BoxCollider2D;
var leftWall : BoxCollider2D;
var rightWall : BoxCollider2D;
//Reference the players
function Start () {
//Move each wall to its edge location:
topWall.size = new Vector2 (mainCam.ScreenToWorldPoint (new Vector3 (Screen.width * 2f, 0f, 0f)).x, 1f);
topWall.center = new Vector2 (0f, mainCam.ScreenToWorldPoint (new Vector3 ( 0f, Screen.height, 0f)).y + 0.5f);
bottomWall.size = new Vector2 (mainCam.ScreenToWorldPoint (new Vector3 (Screen.width * 2, 0f, 0f)).x, 1f);
bottomWall.center = new Vector2 (0f, mainCam.ScreenToWorldPoint (new Vector3( 0f, 0f, 0f)).y - 0.5f);
leftWall.size = new Vector2(1f, mainCam.ScreenToWorldPoint(new Vector3(0f, Screen.height*2f, 0f)).y);;
leftWall.center = new Vector2(mainCam.ScreenToWorldPoint(new Vector3(0f, 0f, 0f)).x - 0.5f, 0f);
rightWall.size = new Vector2(1f, mainCam.ScreenToWorldPoint(new Vector3(0f, Screen.height*2f, 0f)).y);
rightWall.center = new Vector2(mainCam.ScreenToWorldPoint(new Vector3(Screen.width, 0f, 0f)).x + 0.5f, 0f);
}
和玩家一样,这堵墙也附有 RigidBody 2D 和 Box Collider 2D。我的问题是我无法检测到任何类型的碰撞。甚至当我把球拖到墙上时也没有。我打算使用此脚本检测是否在拖动时触摸了墙壁。通过这样做,我将能够调用我的游戏状态“GameOver”并结束游戏。
知道为什么,我无法检测到这种碰撞吗?我确信墙壁的位置是正确的,而且球确实接触到了墙壁。
【问题讨论】: