- Home /
How would i get RaycastHit position in this case?[UPDATED]
-Reformulated question-
Hey guys, i have a problem http://pastebin.com/njgvcgDA if you look at this code, line 26, i try to create an object at the position of the object i selected + x 2. But what happens here is that the location defaults to 0,0,0 + 2,0,0 instead of making the location at the selected object + 2,0,0. How do i fix this?
?? hit.point
is a location. It's the world position Vector3 of where the ray hit (on the target collider.) You're already using it in the Instantiate.
Only thing to watch, as noted below, is not to try to use hit.point
if you didn't hit anything.
Answer by aldonaletto · Mar 10, 2012 at 05:49 PM
I didn't understand the whole logic, but usually you should check if something was hit in the last if prior to use hit.point - you could use the variable test in this case:
if (test == 1 && spawn1 == 1){ var prefab = Instantiate(....);
It looks like he's trying to save the most recent raycast hit, and then spawn at that location later on a button click. $$anonymous$$aybe.
But still same problem -- using the value of hit.point
on a frame when there was no hit.
Your answer
![](https://koobas.hobune.stream/wayback/20220613064952im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Raycast hitting objects to the left of my player 1 Answer
camera detecting walls 1 Answer
RTS CAMERA C# 2 Answers
RTS game: Camera movement is wierd 2 Answers