Unity 5:Raycast没有找到块

时间:2017-08-29 08:40:49

标签: c# unity5 raycasting

嘿伙计们,我在Youtube上发现的一个教程中,一直在研究类似2D terraria的游戏。我一直在使用Quads作为块。该系列的链接是:

https://www.youtube.com/watch?v=KONw5GX0Ixs

唯一的区别是我一直在2D项目而不是3D。我正在尝试实现一个挖掘系统,但点击不会被检测到,而不是我点击的块。系统甚至没有检测到点击,更不用说我点击的块了。我是编码的新手,所以我不知道如何解决这个问题,有什么帮助,谢谢。

我的代码是:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Mining : MonoBehaviour {
    public Vector2 direction;
    void Update() {

    if (Input.GetMouseButtonDown (0)) {
            Vector3 c = Camera.main.ScreenToWorldPoint (Input.mousePosition);

            RaycastHit2D hit2D = Physics2D.Raycast(this.gameObject.transform.position, direction);
            Debug.Log ("hello");

            if (hit2D.collider.gameObject != null) {
                Debug.Log (hit2D.collider.gameObject);
                Destroy (hit2D.collider.gameObject);
            }
    }
    }
}

1 个答案:

答案 0 :(得分:0)

首先,改变

Input.GetMouseButtonDown(0)

Input.GetMouseButton(0) 

作为其

  

返回是否按下给定的鼠标按钮。

告诉我是否能解决您的问题。如果没有,我会试着想出来:)

相关问题