0

I want to click on a 3D plane with my mouse. When I do this, I want it to return a Vector3 of where I clicked. When I use:

Vector3 point = Camera.main.ScreenToWorldPoint(Input.mousePosition);

then, it gives me the Vector3 of the center of the plane. Not what I want. I want it to be at the position I click.

I am trying to create a Ray (Camera.ScreenPointToRay) and work with Physics.Raycast, but that just returns a bool, and not where it actually hits.

I have spent the last 3 hours reading everyone else's questions...what am I missing here?

Evorlor
  • 7,263
  • 17
  • 70
  • 141

2 Answers2

0

Well, you are almost there! What you need to use is Plane.Raycast

Edit

Plane plane = blablabla;
Ray ray = blablabla;
float distance;
Vector3? intersectionPoint = null;
if (plane.Raycast(ray, out distance))
    intersectionPoint = ray.GetPoint(distance);
Sergey Krusch
  • 1,928
  • 16
  • 17
0

Here this should help you…

Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition); //Convert mouse position to raycast
RaycastHit hit; //Create a RaycastHit variable to store the hit data into
Vector3 clickedPosition; //Create a vector3 variable to store the clicked position in

if (Input.GetMouseButtonDown (0) && Physics.Raycast (ray, out hit, 1000)) //If the user clicks the left mouse button and we hit an object with our raycast then
{
    clickedPosition = hit.point; //Store the hit position into the clicked position variable
}

It's that simple :)

Savlon
  • 744
  • 2
  • 11
  • 18