簡體   English   中英

Physics2D.Raycast返回null

[英]Physics2D.Raycast returning null

我有一個問題讓我超級困惑。 我正在從“鍵控代碼”轉到鼠標輸入-之后又變成了觸摸輸入,但是首先我想弄清楚為什么我不能僅用鼠標來完成此操作。

我有一個raycast2d設置-我想讓raycast讀取與我的對象在屏幕上的碰撞。 他們只有在其物體標記為“貓”的情況下才能做出反應-實際上,一旦發生這種情況,貓就會滑出並嘗試攻擊。 但是,它告訴我tagg本身是實例化的引用。 但是對象本身默認存在,所以我不確定在這里做什么。 這是我的整個劇本。

 void Update() {
    //if ((Input.GetKeyDown(KeyCode.O) && !attacking && attackTimer <= 0)) {
    if (Input.GetMouseButtonDown(0) && !attacking && attackTimer <= 0) {  //every frame check to see if the mouse has been clicked.
                                                                          //Get the mouse position on the screen and send a raycast into the game world from that position.
        Vector2 worldPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition);//Vector 2 means only 2 points of axis are read. worldpoint means check the point of the world. Camera is used to determien what we are looking at, and we fill it with our mouse location.
        RaycastHit2D hit = Physics2D.Raycast(worldPoint, Vector2.zero);
        if (hit.collider == null) {//THIS IS THE LINE that says it is having issues. If i REMOVE this line, ANYWHERE i click on the screen activates the cat and thats not what I want to happen.

            Debug.Log("No Object Touched");

            if (hit.collider.CompareTag("Cat")) {
                GameManager.Instance.AudioSource.PlayOneShot(SoundManager.Instance.Swipe);

                attacking = true;
                attackTimer = attackCd;

                attackTrigger.enabled = true;
            }

更新的代碼以匹配所需的更改:我現在收到的錯誤是NullreferenceException-用於:

 if (hit.collider.CompareTag("Cat")) {

在使用程序員建議測試的方法重新測試之后,這與我之前遇到的錯誤相同。

控制台將向我顯示我還沒有單擊任何對象,然后向我顯示null。 因此,我想它試圖告訴我它沒有找到場景中存在的任何標記為貓的東西? 即使Cat是添加的自定義標簽,它也已通過Box對撞機應用於貓的游戲對象。 是否需要任何材料或任何材料來閱讀它的存在? 單擊其特定位置后,還有其他方法可以調用該對象嗎?

更新:

       Debug.Log("No Object Touched");
                if (hit.collider) {
                    if (hit.collider.tag == "cat1") { - 

這擺脫了空引用,但它根本不讀貓。 如果我點擊那只貓,那什么也沒發生。 是的,現在在編輯器中將其正確標記為“ cat1”。 Meanign標簽-新的自定義標簽,創建了cat1標簽。 轉到游戲對象,將標簽更改為cat1。 還確保了大腸菌已被觸發。

經過數小時的搜索,我終於找到了導致null的原因。

您需要將“新組件”>“物理”>“ Box Collider”附加到您的Sprite游戲對象。

現在,您可以在添加到游戲對象的腳本中使用以下代碼片段來了解是否單擊了該對象:

private Ray ray;
private RaycastHit hit;

private void Update()
{
    if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began) 
    {
        ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);

        if (Physics.Raycast(ray, out hit, Mathf.Infinity))
        {
            if (hit.transform.gameObject.name == "YourGameObject")
            {
                // Do your stuff on click
            }
        }
    }
}

這就是3d對象可以輕松檢測到觸摸的原因,因為在創建3d對象時已經附加了碰撞器。

希望這個答案對某人有幫助。

首先, Physics.RaycastPhysics2D.Raycast是兩個不同的東西。

當某物被擊中時Physics.Raycast返回true ,而RaycastHit2D返回RaycastHit2D因此在使用Physics2D.Raycast時必須檢查null ,否則將獲得NullPointerException異常。

if (hit == null)
{
    Debug.Log("No Object Hit");
    //Return
    return;
}

我將為diff貓設置diff標簽,因此cat,cat1,cat2,cat3。 並且每個腳本將分別與該標簽相關。

但是為什么要這樣做: if (hit.collider.tag == "Cat")因為您沒有提到您有一個名為Cattag

請記住,您列出的cat標記中的C 都不大寫。

那應該是if (hit.collider.tag == "cat" || hit.collider.tag == "cat1" || hit.collider.tag == "cat2" || hit.collider.tag == "cat3")

如果每只貓在做不同的事情,那么您應該這樣做:

if (hit == null)
{
    Debug.Log("No Object Hit");
    //Return
    return;
}

if (hit.collider.CompareTag("cat"))
{

}
else if (hit.collider.CompareTag("cat1"))
{

}
else if (hit.collider.CompareTag("cat2"))
{

}
else if (hit.collider.CompareTag("cat3"))
{

}

上面提到的null檢查和Cat拼寫可能是導致問題的原因。 請記住,您沒有提到要開始的錯誤。

您不知道如何使用標簽 這里

我建議你用

hit.collider.CompareTag("Cat")

代替

hit.collider.tag == "Cat"

並確保在編輯器中設置了“貓”標簽

優良作法是先檢查匹配是否具有對撞機,然后確認它是您要比較標記的對象。

 if (hit.collider) {
     // The hit has a collider
     if (hit.collider.tag=="Cat") {
         Debug.Log("Touched it!");
     }
 }

在統一論壇中: http : //answers.unity3d.com/questions/474523/how-can-i-use-hitgameobjecttag.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM