Reputation: 2562
I was following a tutorial for Unity3D that was written in C# and was attempting to do it in UnityScript. The following line is the only one that I couldn't convert correctly and it has to do with Raycasting.
Here's the C# from the tut.
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
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.
Upvotes: 1
Views: 1342
Reputation: 17023
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)
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.
}
Notice, according to the LayerMask
documentation, LayerMask
can be implicitly converted from an integer.
Upvotes: 1
Reputation: 63772
It doesn't need the out keyword at all.
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
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.
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.
In UnityScript, this is unnecessary, and you'll get what you expect without using the out
keyword.
Upvotes: 1
Reputation: 32702
out means that the parameter passed to it will be passed "by reference", as opposed to "by value". See C# documentation.
I'm not familiar with 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.
class MyCustomReturnObject
Boolean OriginalReturnVariable
RaycastHit hit
Upvotes: 2