I realize that the unity tutorial might be confusing since you are new to rayCasting.
I took some time to build a little scene where I have a plane, and everywhere I click appears
a cube(prefab).
here is the code I used along with some comments to help you understand what the code does :
using UnityEngine;
using System.Collections;
public class RaycastScript : MonoBehaviour{
Ray myRay; // initializing the ray
RaycastHit hit; // initializing the raycasthit
public GameObject objectToinstantiate;
void Update ()
{
myRay = Camera.main.ScreenPointToRay (Input.mousePosition); // telling my ray variable that the ray will go from the center of
// my main camera to my mouse (This will give me a direction)
if (Physics.Raycast (myRay, out hit)) { // here I ask : if myRay hits something, store all the info you can find in the raycasthit varible.
// things like the position where the hit happend, the name of the object that got hit etc…etc…
if (Input.GetMouseButtonDown (0)) {// what to do if i press the left mouse button
Instantiate (objectToinstantiate, hit.point, Quaternion.identity);// instatiate a prefab on the position where the ray hits the floor.
Debug.Log (hit.point);// debugs the vector3 of the position where I clicked
}// end upMousebutton
}// end physics.raycast
}// end Update method
}// end class
note : make sure you have a floor or so that has a collider… The ray ignore everything that has no collider.
also if you instantiate a cube on the place the ray hits the plane, the cube will be half on top of the plane, half under it. a quick fix for that is to add a vector3 to the hit.point to make the cube rest on top of the plane . eg : instead of : Instantiate (objectToinstantiate, hit.point, Quaternion.identity); you might wanna do Instantiate (objectToinstantiate, hit.point + new vector3(0,0.5f,0), Quaternion.identity);
I hope I clarified the rayCasting a little bit and good luck with your project !