我正在关注用C#编写的Unity3D教程,并试图在UnityScript中进行。下一行是我无法正确转换的唯一一行,它与Raycasting有关。

这是来自tut的C#。

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


这是我的相关变量。

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


这是Physics.Raycast的签名

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


我知道我的问题是使用UnityScript不能识别出什么是“出”,并且我不知道该用什么代替。

最佳答案

根据documentation

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


参数(阅读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. In UnityScript, you don't need to define when to pass by reference or value. So, you simply omit the 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.
}


注意,根据LayerMask documentationLayerMask可以从整数隐式转换。

关于c# - Raycast-从C#转换为UnityScript,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20999330/

10-13 06:56