C# 使用Raycasts2D Unity C获取错误#

C# 使用Raycasts2D Unity C获取错误#,c#,unity3d,C#,Unity3d,我正在从事一个项目,该项目使用光线投射作为拍摄机制的一部分。由于某些原因,虽然我在使用光线投射时会出现错误,但我不确定如何修复它们。这是我的密码: void Shoot(){ RaycastHit2D hit; if (Physics2D.Raycast(player.position, player.forward, out hit, weapon.range, targetMask)) { Debug.Log ("You Hit: " + hit.colli

我正在从事一个项目,该项目使用光线投射作为拍摄机制的一部分。由于某些原因,虽然我在使用光线投射时会出现错误,但我不确定如何修复它们。这是我的密码:

void Shoot(){
    RaycastHit2D hit;

    if (Physics2D.Raycast(player.position, player.forward, out hit, weapon.range, targetMask)) {
        Debug.Log ("You Hit: " + hit.collider.name);
    }
}
我得到的错误是:

Assets/Scripts/Shooting.cs(26,17):错误CS1502:UnityEngine.Physics2D.Raycast(UnityEngine.Vector2,UnityEngine.Vector2,float,int,float)的最佳重载方法匹配具有一些无效参数

Assets/Scripts/Shooting.cs(26,62):错误CS1615:参数“#3”不需要“out”修饰符。考虑删除“外出”修饰符< /P> 感谢您提供的任何建议,原因可能是……

可能不清楚您可以使用什么。以下是所有可用的方法:

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
修改器。

(简而言之,您使用的是一种不存在的方法,它特别抱怨
出击

它们都返回一个结构。这意味着您不能将其与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);
    }
}
用户可能不清楚您可以使用什么。以下是所有可用的方法:

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
修改器。

(简而言之,您使用的是一种不存在的方法,它特别抱怨
出击

它们都返回一个结构。这意味着您不能将其与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);
    }
}
欢迎使用堆栈溢出:)。看来你还没有读到错误信息?请参阅CS1502错误-它提供了它所期望的签名,并且不期望参数#3处出现
RaycastHit2D
,您给出了该参数。您可能完全使用了错误的函数。检查传入的内容,并检查该函数的Unity脚本文档。欢迎使用堆栈溢出:)。看来你还没有读到错误信息?请参阅CS1502错误-它提供了它所期望的签名,并且不期望参数#3处出现
RaycastHit2D
,您给出了该参数。您可能完全使用了错误的函数。检查传入的内容,并检查该函数的Unity脚本文档。