- Home /
Raycast not working with instantiated objects.
Hello, this is my first question on the forums. I was looking all over the internet,but I found no answer. I am making a game, which should generate land as you walk. Heres my code: var land : GameObject; var xpos : float; var zpos : float; var xuse : float; var zuse : float;
function Update () {
xpos = transform.position.x / 50;
zpos = transform.position.z / 50;
xuse = Mathf.Round(xpos) * 50;
zuse = Mathf.Round(zpos) * 50;
var hit : RaycastHit;
if (!Physics.Raycast (Vector3(xuse,1,zuse), Vector3(xuse,-1,zuse), hit, 5.0)) {
Instantiate(land,Vector3(xuse,0,zuse),Quaternion.identity);
print("Land instantiated");
}
else {
if (hit.transform.tag == "Land") {
print("Land found");
}
}
}
This might not be most efficent way to do it, but anyways. So when I walk it generates ground below me on a grid of 50. The problem is when it raycasts and finds nothing below it generates ground, but it doesnt detect it with raycast so it keeps infinitley generating it. I found on another post that checking for tags, but this isn't working since its kind of opposite. I really have no idea what to do. Waiting for any answers.
Does the new land you create have a collider? Raycast() only works with objects that have colliders. If it does have a collider, then the next thing is to do a Debug.DrawRay() to verify that your Raycast() is going where you expect it to.
Yes the object has a collider. I just tested, and the raycast is going somewhere crazy.. hmm.. Thanks ! I'll try to figure it out now. I'll say if I need anymore help.
Can you explain your logic a little bit? What is xuse and yuse [conceptually] ?
Okay, I looked at code, and I have no idea. The thing is that it should be working cause they both have same position set except Y, but for some reason it now has raycast start from x50 y1 z0 but go to x100 y-1 z0. Thats weird cause they both have the same x set.
Answer by Thewoxyz · Jul 18, 2013 at 07:02 PM
Okay, all fixed :) Thanks to robertbu, I tried using Debug.DrawRay and I found out my ray was facing completely wrong direction. Everything is fixed now. If anyone is interested in the script then here ya go: var land : GameObject; var xpos : float; var zpos : float; var xuse : float; var zuse : float; var tr : Vector3;
function Update () {
xpos = transform.position.x / 50;
zpos = transform.position.z / 50;
xuse = Mathf.Round(xpos) * 50;
zuse = Mathf.Round(zpos) * 50;
tr = Vector3(xuse,1,zuse);
var hit : RaycastHit;
if (!Physics.Raycast (tr, Vector3(0,-1,0), hit, 5.0)) {
Instantiate(land,Vector3(xuse,0,zuse),Quaternion.identity);
print("Land instantiated");
Debug.DrawRay(tr, Vector3(0,-1,0), Color.white, 5.0);
}
else {
print("Something in the way");
Debug.DrawRay(tr, Vector3(0,-1,0), Color.white, 5.0);
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613110356im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Instantiate a little before the end of the raycast or on screen 0 Answers
Instantiate Projector at Normal Direction 0 Answers
Raycast Specific Object/Instantiate Explosion 1 Answer
Instantiate prefab from original position to click mouse point position 0 Answers
Instantiate gameobject a certain distance away from transform 2 Answers