Here’s the script:
#pragma strict
var hitPosition : Vector3;
var test : Transform;
function Update () {
if (Input.GetMouseButtonUp(0)) {
Instantiate (test, hitPosition, Quaternion.identity);
var hit: RaycastHit;
var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast (ray, hit)) {
hitPosition = hit.point;
}
}
}
for some reason it doesn’t always spawn where you click but it spawns where you previously click. So if you clicked it would spawn in that location, if you click in a different place it will spawn in the previous place etc.
Any help?
Just got the order of what you’re doing wrong, try this
#pragma strict
var hitPosition : Vector3;
var test : Transform;
function Update () {
if (Input.GetMouseButtonUp(0)) {
var hit: RaycastHit;
var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast (ray, hit)) {
hitPosition = hit.point;
Debug.Log(hitPosition);
Instantiate (test, hitPosition, Quaternion.identity);
}
}
}