【问题标题】:How to detect collisions when using a GameObject collides with a BoxCollider 2D while being dragged on Unity 3D如何在 Unity 3D 上拖动时使用 GameObject 与 BoxCollider 2D 碰撞时检测碰撞
【发布时间】: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”并结束游戏。

知道为什么,我无法检测到这种碰撞吗?我确信墙壁的位置是正确的,而且球确实接触到了墙壁。

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    您只是在问如何检测碰撞?确保所有对象(墙壁和玩家对象)都具有 Collider2D 组件。在这些碰撞器上,将 IsTrigger 设置为 true。然后在你的播放器控制器中:

    void OnTriggerEnter2D(Collider2D other) {
        // check if "other" is a wall
        if (other.transform.GetComponent<Wall>()) 
            this.gameState = GameStates.GAMEOVER;
    }
    

    请注意,您需要在至少一个可碰撞游戏对象上使用 RigidBody2D。只需将 RigidBody2D 放在所有东西上,不要使用重力。

    【讨论】:

    • 正如我在问题上所说,碰撞对我不起作用,因为 RigidBody 或 Collision2D 只是检测碰撞,但使用我的拖动脚本它们实际上并没有碰撞。
    • 你没有显示你在哪里检测到碰撞。他们绝对应该碰撞。如果您没有收到碰撞,那就是您需要开始的地方。
    【解决方案2】:

    你没有写出你是如何制作screenPoint的,所以这里只是装箱

    public void OnMouseDown() {
        screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-12
      • 1970-01-01
      • 1970-01-01
      • 2015-12-04
      • 1970-01-01
      相关资源
      最近更新 更多