I’ve been trying the methods described here: 1.
But this is giving me a lot of errors. I can’t retrieve the objects I need for these functions. The exact method described there gives me errors saying that the objects don’t exist.
If I then do GameObject.FindGameObjectWithTag() I will get errors saying that GameObject.FindGameObjectWithTag does not have the functions described in my link.
If I do a GetComponent<>, it can’t find the objects.
Any idea what I could try?
EDIT: The method that seems to be giving the least errors is:
if (plane.Raycast(mainCamera.ScreenPointToRay(Input.mousePosition),enter))
{
drawingPosition = mainCamera.ScreenPointToRay(Input.mousePosition).GetPoint(enter);
}
But I can’t figure out what I should fill in in the “out float enter” parameter. The documentation isn’t particularly helpful either; it doesn’t even mention that parameter (2)
var rodentButton : int = 0 ; //set to left mouse button with '0', can change in inspector
var rodentRange : float = 250.0 ; //set in inspector how far into 3d space from the screen you can click
var tagger : String = "MyPlaneTag" ; //set this in inspector to whatever you tag your plane/whatever
function Update(){
if(Input.GetMouseButtonDown(rodentButton)){
FindTheCheese() ;
}
}
function FindTheCheese(){
var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition) ; //make sure you have a camera in the scene tagged as 'MainCamera'
var hit : RaycastHit ;
if(Physics.Raycast(ray, hit, rodentRange)){
if(hit.transform.CompareTag(tagger)){
//do whatever you're wanting to do here...
Debug.Log("World coordinates of our mouse click = " +hit.point) ; //a debug so you can see what it's returning. "hit.point" here is what returns the coordinates
}
}
}
Edit
I'm not too sure about C# or splitting up functions in it, but here's my best guess at translating the above and just putting it all into the update() ->
using UnityEngine;
using System.Collections;
public class NameItWhatever : MonoBehaviour {
public int rodentButton = 0;
public float rodentRange = 250.0f;
public string tagger = "MyPlaneTag";
void Update(){
if(Input.GetMouseButtonDown(rodentButton)){
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if(Physics.Raycast(ray, out hit, rodentRange)){
if(hit.transform.CompareTag(tagger)){
//do whatever......
Debug.Log(hit.point);
}
}
}
}
}
Intuitively you put the equation of your ray into the plane equation to solve for the distance from the origin the line intersects the plane, then plug that back into the ray equation to find the point on the plane.