Can't detect a plane with Raycast (not unity plane asset but the "public Plane plane;" sort of thing)
I am trying to create a touch-camera-panning-control through the use of what I have been calling 3D input. The theory goes as follows:
On the beginning of the touch a ray will be cast unto a plane (facing up/horizontal). The ray will be cast as a screenPointToRay from Input.GetTouch(0).position. The point where the ray collides with the plane will be recorded as "oldPoint" on the .moved phase of touch(0) another ray will be cast like the first and the intersection point recorded as "currentPoint". The difference between these points is the distance your finger has moved... relative to a 3D horizontal plane. [I am trying to achieve what others have called pixel perfect panning; if i were to touch a mountain on the screen and make a panning motion, the mountain would remain under my finger tip the whole time] The camera will be moved according to this distance. oldPoint is then set = currentPoint and a new point is recorded and assigned as the currentPoint as the cycle repeats and grants me my "pixel perfect panning" as I so dearly wish to achieve.
my problem lies in the first step, hindering the entirety of my genius scheme; I am trying to detect the raycast panRay's intersection with the plane panPlane using raycast, but it is not working.
public Plane panPlane;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.touchCount == 1)
{
if (Input.GetTouch(0).phase == TouchPhase.Began)
{
//panPlane.SetNormalAndPosition(new Vector3(0, 0, 0), Vector3.up);
Ray panRay = GetComponent<Camera>().ScreenPointToRay(Input.GetTouch(0).position);
float hitDist;
if (panPlane.Raycast(panRay, out hitDist))
{
Debug.Log("Hit");
}
}
if (Input.GetTouch(0).phase == TouchPhase.Moved)
{
}
if (Input.GetTouch(0).phase == TouchPhase.Ended)
{
}
}
The if statement handling the detection of the ray is not returning true when i tap on the screen and ive been trying for hours in vain to discover why. I've spent hours on the api manual and it seems to me that all my syntax is correct; thoroughly stumping me.
Please help! Much appreciated!!!!!
P.S. if you see any flaws in my master plan feel free to point them out, if there is a better way to achieve this affect (with a perspective camera) PLEASE tell me!!!
Answer by Vinnie420 · Apr 10, 2016 at 11:43 AM
i think it is because you are using the "Plane" class, wich is not an actual plane, but more of a mathematical representation of a plane. (not exactly sure) If you have a plane as a gameobject in the scene, try changing "public Plane panPlane" to "public GameObject panPlane"
Your answer
Follow this Question
Related Questions
3D click and drag Camera Movement is shaking rapidly 0 Answers
Unity Error CS1729 The Type 'Plane' does not contain a constructer that takes 2 arguments 0 Answers
Camera Panning fail - erratic movement and then stops 1 Answer
Determining point coordinates on a plane for a known normal 1 Answer
Problem with Vector3.MoveTowards 2 Answers