简体   繁体   English

Raycast-从C#转换为UnityScript

[英]Raycast - convert from C# to UnityScript

I was following a tutorial for Unity3D that was written in C# and was attempting to do it in UnityScript. 我正在关注用C#编写的Unity3D教程,并试图在UnityScript中进行。 The following line is the only one that I couldn't convert correctly and it has to do with Raycasting. 下一行是我无法正确转换的唯一行,它与Raycasting有关。

Here's the C# from the tut. 这是来自tut的C#。

if(Physics.Raycast(ray, out hit, Mathf.Abs(deltaY), collisionMask)){
    ....
}

Here are my relevant variables that I have. 这是我的相关变量。

var ray : Ray;
var hit : RaycastHit;
var collisionMask : LayerMask;
var deltaY : float = moveAmount.y; // moveAmount is a Vector2

Here is the signature for Physics.Raycast 这是Physics.Raycast的签名

function Raycast (origin : Vector3,
                  direction : Vector3,
                  distance : float = Mathf.Infinity,
                  layerMask : int = kDefaultRaycastLayers) : boolean

I know that my problem is that using UnityScript doesn't recognize what 'out' is and I don't know what to substitute in its place. 我知道我的问题是使用UnityScript无法识别出什么是“出”,并且我不知道该用什么代替。

out means that the parameter passed to it will be passed "by reference", as opposed to "by value". out表示传递给它的参数将“按引用”传递,而不是“按值”传递。 See C# documentation . 请参阅C#文档

I'm not familiar with UnityScript. 我对UnityScript不熟悉。 But one way to create an equivalent function would be to create a new class to be returned that contains the Boolean that would normally be returned, as well as the RaycastHit object. 但是创建等效功能的一种方法是创建一个要返回的新类,其中包含通常会返回的布尔值以及RaycastHit对象。

class MyCustomReturnObject
    Boolean OriginalReturnVariable
    RaycastHit hit

It doesn't need the out keyword at all. 它根本不需要out关键字。

var hit : RaycastHit;
if (Physics.Raycast (origin, direction, hit, distance)) {
    var hitDistance = hit.distance;
}

You can use the UnityScript docs for reference: http://docs.unity3d.com/Documentation/ScriptReference/Physics.Raycast.html 您可以使用UnityScript文档作为参考: http : //docs.unity3d.com/Documentation/ScriptReference/Physics.Raycast.html

out and ref keywords are useful C# constructs that allow you to pass a reference to a structure (or a reference to a class reference), which would usually be passed by value instead. outref关键字是有用的C#构造,可让您将对结构的引用(或对类引用的引用)传递给值,通常将其传递给值。

The C# variant of the method would create a new instance of the RaycastHit type and assign it to the variable you pass as an out parameter. 该方法的C#变体将创建RaycastHit类型的新实例,并将其分配给您作为out参数传递的变量。

In UnityScript, this is unnecessary, and you'll get what you expect without using the out keyword. 在UnityScript中,这是不必要的,无需使用out关键字即可获得期望的out

According to the documentation : 根据文档

static function Raycast(ray: Ray,
                        hitInfo: RaycastHit,
                        distance: float = Mathf.Infinity,
                        layerMask: int = DefaultRaycastLayers): bool;

Parameters (read the description for hitInfo) 参数(阅读hitInfo的描述)

ray          The starting point and direction of the ray.
distance     The length of the ray.
hitInfo      If true is returned, hitInfo will contain more information about where the 
             collider was hit (See Also: RaycastHit).
layerMask    A Layer mask that is used to selectively ignore colliders when casting a ray.

In C#, out passes by reference, which allows the function to modify the value outside its scope. 在C#中, out通过引用传递,这允许函数在其范围之外修改值。 In UnityScript , you don't need to define when to pass by reference or value. UnityScript ,您无需定义何时通过引用或值传递。 So, you simply omit the out . 因此,您只需省略out

var ray : Ray;
var hit : RaycastHit;
var collisionMask : LayerMask;
var deltaY : float = moveAmount.y; // moveAmount is a Vector2

if (Physics.Raycast(ray, hit, deltaY, collisionMask)) {
    //hit will contain more information about whether the collider was a hit here.
}

Notice, according to the LayerMask documentation , LayerMask can be implicitly converted from an integer. 注意,根据LayerMask文档LayerMask可以从整数隐式转换。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM