How to use ray plane intersection & update point vector 3?

I want to use a ray like a line and update the end point vector 3 when a plane intersects the ray.

Is it possible to make a new point from the ray intersection and change it if the plane intersects?

Ray.GetPoint is not changing with each update.

``````using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlaneCast : MonoBehaviour
{
public Ray r;

public Vector3 P3;

public GameObject PointA;

public GameObject PointB;

public float LineDistance;

// Start is called before the first frame update
void Start()
{
P3 = new Vector3();

quadPlane = new Plane();

Mesh mesh = GetComponent<MeshFilter>().sharedMesh;

Vector3[] normals = mesh.normals;

}

// Update is called once per frame
void Update()
{
Vector3 P1 = PointA.transform.position;

Vector3 P2 = PointB.transform.position;

Vector3 tp1 = transform.TransformPoint(P1);

Vector3 tp2 = transform.TransformPoint(P2);

Debug.DrawLine(P1, P2);

r = new Ray(tp1, tp2);

//LineDistance = Vector3.Distance(tp1, tp2);

if (quadPlane.Raycast(r, out LineDistance))
{
print(r.GetPoint(LineDistance));
Debug.Log("Intersecting plane");
}

P3 = r.GetPoint(LineDistance);
Debug.Log(LineDistance);
}
}
``````

Well, first of all the way you create the Ray struct is already flawed. Have a look at the constructor. It does not take two points but one point (the origin of the ray) and a direction vector. You currently pass your second point as direction vector. You probably want to pass `tp2 -tp1` as direction.

As for the rest of your code, using LineDistance outside the if statement has issues if the ray doesn’t hit anything. Since you use an infinite plane it shouldn’t be a huge issue, but generally a flawed approach.

The ray is infinite in one direction.

I’m wanting to use ray plane intersection like line plane intersection.

I changed the if statement to check the distance to the line.

``````using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RayPlaneIntersection : MonoBehaviour
{
public Ray r;

public GameObject PointA;

public GameObject PointB;

public float MaxDistance;

public float LineDistance;

public Vector3 IntersectionPoint;

public GameObject PlaneGameObject;

// Start is called before the first frame update
void Start()
{

}

// Update is called once per frame
void Update()
{
Vector3 P1 = PointA.transform.position;

Vector3 P2 = PointB.transform.position;

Debug.DrawRay(P1, P2 - P1, Color.blue);

r = new Ray(P1, P2 - P1);

MaxDistance = Vector3.Distance(P1, P2);

PlaneGameObject.GetComponent<PlaneObject>().ObjectPlane.Raycast(r, out LineDistance);

if (LineDistance >= 0 && LineDistance <= MaxDistance)
{
IntersectionPoint = r.GetPoint(LineDistance);
print(r.GetPoint(LineDistance));
Debug.Log("Intersecting line");
}
}
}
``````