簡體   English   中英

使用Raycasts2D Unity C#出錯

[英]Getting Errors with Raycasts2D Unity C#

我正在做一個使用射線廣播作為射擊技師一部分的項目。 由於某些原因,盡管在使用射線投射時出現錯誤,但我不確定如何修復它們。 這是我的代碼:

void Shoot(){
    RaycastHit2D hit;

    if (Physics2D.Raycast(player.position, player.forward, out hit, weapon.range, targetMask)) {
        Debug.Log ("You Hit: " + hit.collider.name);
    }
}

我得到的錯誤是:

資產/腳本/Shooting.cs(26,17):錯誤CS1502:“ UnityEngine.Physics2D.Raycast(UnityEngine.Vector2,UnityEngine.Vector2,float,int,float)”的最佳重載方法匹配具有一些無效的參數

資產/腳本/Shooting.cs(26,62):錯誤CS1615:參數'#3'不需要'out'修飾符。 考慮刪除“ out”修飾符

感謝您提出的任何建議,為什么會這樣...

2D RaycastUnity文檔可能不清楚您可以使用什么。 這是所有可用的方法:

Raycast(Vector2 origin, Vector2 direction) : RaycastHit2D

Raycast(Vector2 origin, Vector2 direction, float distance) : RaycastHit2D

Raycast(Vector2 origin, Vector2 direction, float distance, int layerMask) : RaycastHit2D

Raycast(Vector2 origin, Vector2 direction, float distance, int layerMask, float minDepth) : RaycastHit2D

Raycast(Vector2 origin, Vector2 direction, float distance, int layerMask, float minDepth, float maxDepth) : RaycastHit2D

與3D射線廣播不同, 沒有可用的重載使用out修飾符。

(簡而言之,您正在使用一種不存在的方法,它專門抱怨被out hit

它們都返回RaycastHit2D 結構 這意味着您無法將其與null進行比較以查看匹配是否有效。 因此,根據您的變量名稱,我認為您的意思是:

void Shoot(){
    RaycastHit2D hit;

    hit = Physics2D.Raycast(player.position, player.forward, weapon.range, targetMask);

    // As suggested by the documentation, 
    // test collider (because hit is a struct and can't ever be null):
    if(hit.collider != null){
        Debug.Log ("You Hit: " + hit.collider.name);
    }
}

暫無
暫無

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

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