Reputation: 3
I'm new to c# and trying to figure out how to get raycasting to work here in Unity3d's new 2d support.
I'm getting the error "cannot convert from 'UnityEngine.Ray2D' to 'UnityEngine.Vector2'"
for (int i = 0; i<3; i ++) {
float dir = Mathf.Sign(deltaY);
float x = (p.x + c.x - s.x/2) + s.x/2 * i;
float y = p.y + c.y + s.y/2 * dir;
ray = new Ray2D(new Vector3(x, y), new Vector3(0, dir));
Debug.DrawRay(ray.origin, ray.direction);
hit = Physics2D.Raycast(ray.origin, ray.direction, Mathf.Abs(deltaY), collisionMask);
if (hit != null && hit.collider != null)
{
}
if (Physics2D.Raycast(ray,out hit,Mathf.Abs(deltaY),collisionMask)) {
float dst = Vector2.Distance (ray.origin, hit.point);
if (dst > skin) {
deltaY = dst * dir + skin;
}
else {
deltaY = 0;
}
grounded = true;
break;
}
}
Can someone help me out?
Upvotes: 0
Views: 12286
Reputation: 2016
Do call like you did in code earlier:
hit = Physics2D.Raycast(ray.origin, ray.direction, Mathf.Abs(deltaY), collisionMask);
This code is useless:
if (hit != null && hit.collider != null) { }
But if you need to enter some logics in brackets - you don't need to check hit for null - it is always an object, just check hit.collider
Upvotes: 0
Reputation: 4356
See the Physics2D.Raycast
documentation, the method is defined as
static RaycastHit2D Raycast(Vector2 origin, Vector2 direction, float distance = Mathf.Infinity, int layerMask = DefaultRaycastLayers, float minDepth = -Mathf.Infinity, float maxDepth = Mathf.Infinity);
With the parameters defined as
origin: The point in 2D space where the ray originates.
direction: Vector representing the direction of the ray.
distance: Maximum distance over which to cast the ray.
layerMask: Filter to detect Colliders only on certain layers.
minDepth: Only include objects with a Z coordinate (depth) greater than this value.
maxDepth: Only include objects with a Z coordinate (depth) less than this value.
You are using a Ray2D
instead of a Vector2D
for origin
. If you simply replace ray
with a Vector2D
it will work
Physics2D.Raycast(new Vector2(x, y),out hit,Mathf.Abs(deltaY),collisionMask)
For more information, see passing parameters
Upvotes: 4