- Home /
Question by
Chickenator · Jan 28, 2015 at 08:03 PM ·
javascriptraycastraycastingraycasthit
Raycast script help?
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?
Comment
Best Answer
Answer by Mmmpies · Jan 28, 2015 at 08:23 PM
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);
}
}
}
Your answer
Follow this Question
Related Questions
Make tranform follow raycast 1 Answer
Help with Raycast C# 0 Answers
Raycast exit point of collider 0 Answers
Another Raycast Issue. 0 Answers
Raycast Not Drawing In Target Direction 0 Answers