Can't call raycast on plane to get mouse position? Beginner Question
I'm trying to run the sample code from the API here: http://docs.unity3d.com/ScriptReference/Plane.Raycast.html
But it doesn't run. Why can't I call .Raycast on the groundPlane?
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour {
public Plane groundPlane;
public Transform markerObject;
void Update() {
if (Input.GetMouseButtonDown(0)) {
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
float rayDistance;
if (groundPlane.Raycast(ray, out rayDistance))
markerObject.position = ray.GetPoint(rayDistance);
}
}
}
Yields the following error: Assets/Scripts/Player/old/ExampleClass.cs(11,41): error CS1061: Type Plane' does not contain a definition for
Raycast' and no extension method Raycast' of type
Plane' could be found (are you missing a using directive or an assembly reference?)
Some online tutorial code also yields a similar set of errors? For starters, when initializing the plane, I can't pass the two parameters, when the API says I can?
Ray ray = viewCamera.ScreenPointToRay (Input.mousePosition);
Plane groundPlane = new Plane (Vector3.up, Vector3.zero);
float rayDistance;
if (groundPlane.Raycast(ray,out rayDistance)) {
Vector3 point = ray.GetPoint(rayDistance);
Debug.DrawLine(ray.origin,point,Color.red);
//Debug.DrawRay(ray.origin,ray.direction * 100,Color.red);
controller.LookAt(point);
}
Answer by FortisVenaliter · Jun 08, 2016 at 07:47 PM
Hmmm... I just checked, and the function does exist... and it should work with the code you've provided. Do you have any other scripts called "Plane" that may be conflicting?
Also, I note that the file you are editing is in: Assets/Scripts/Player/old/
Could that be a file you tried to deprecate?
Wow, utterly total rookie mistake. Had an old script named Plane hidden in a folder above (was trying to make a flying plane to learn Unity!).