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;

public class PlaneManipulation : MonoBehaviour {
    Ray ray;
    RaycastHit hit;

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

    public LayerMask planeLayer;

    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;

    void Update() {

  • \$\begingroup\$ i added the same layer for both the planes and it is working. why? \$\endgroup\$ Feb 23 '15 at 0:37
  • \$\begingroup\$ It's working for me as well OP. Did you make sure that you assigned the correct layers to your planes? \$\endgroup\$
    – 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.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.