# Raycasting from vertices is not working

I have two perpendicular planes in xz and yz like this

I want to find the projection of the vertices of plane 2 on plane 1 using raycasting.The rays are hitting the plane but the log is showing zeros. why?

using UnityEngine;
using System.Collections.Generic;

//[ExecuteInEditMode()]
public class PlaneManipulation : MonoBehaviour {
Ray ray;
RaycastHit hit;

Mesh mesh;
private Vector3[] vertices;
public float rayLength;

void ChangeMesh() {
mesh = GetComponent<MeshFilter>().sharedMesh;
vertices = mesh.vertices;
//using only the first row of the vertices from the plane
for (int i = 0; i < 11; i++)
{
Vector3 vert = transform.TransformPoint(vertices[i]);

Physics.Linecast(vert, Vector3.up, out hit,planeLayer);

Debug.DrawRay(vert, Vector3.up);

Debug.Log("hitPoint = " + hit.point);

//vert.y = hit.point.y;
//vertices[i].y = hit.point.y;
//vertices[i] = transform.InverseTransformPoint(vert);
}

mesh.vertices = vertices;
mesh.RecalculateNormals();
mesh.RecalculateBounds();
}

void Update() {

ChangeMesh();
}
}

• i added the same layer for both the planes and it is working. why? – aditya sarma Feb 23 '15 at 0:37
• It's working for me as well OP. Did you make sure that you assigned the correct layers to your planes? – fryBender Feb 23 '15 at 15:53

You're not raycasting, you're linecasting:

Physics.Linecast(vert, Vector3.up, out hit,planeLayer);

origin | destination | hit info | layer mask


The second argument here is the end of the line you want to check along, not the direction of the line. So you're checking for an intersection along the line from your vertex to the point (0, 1, 0) in world space - probably not what you intend.

Check the docs on Linecast versus Raycast to confirm.

I think what you mean to write is:

Physics.Raycast (vert, Vector3.up, out hit, Mathf.Infinity, planeLayer);

origin | direction | hit info | max distance | layer mask


I've labelled the arguments here because implicit conversion between layer masks and numbers makes it easy to accidentally get the wrong override without any errors or warnings.