- Home /
Raycast textureCoord
Hello, I've been working with Unity3D for a couple of days now, and I'm stuck! I use the following code to create a platform using 1x1x1 cubes:
function Start() { for (var x : int = 0; x < 40; x++) { for (var y : int = 0; y < 40; y++) { count ++;
block = Instantiate (BlockPrefab, Vector3(1.0 + (1.0 * x), 0.0, 1.0 + (1.0 * y)), Quaternion.identity) as Transform;
block.name = "block_" + count;
//Debug.Log(block.name);
}
}
}
This works just fine. Now what I want to achieve is being able to left click my mouse on a cube (I call them blocks in script) then create an other top right on top of the cube I clicked (my ultimate goal is being also able to click on a side of a cube and it will create a cube on that side), this is what I've got so far:
function Update () { if(Input.GetButtonDown("Fire1")) { var hit : RaycastHit;
var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast (ray, hit))
{
var newPosition = Vector3(hit.point.x, hit.point.y+1, hit.point.z);
block = Instantiate (BlockPrefab, newPosition, Quaternion.identity);
count ++;
block.name = "block_" + count;
Debug.Log(hit.collider.gameObject.name);
//Debug.Log(hit.textureCoord);
}
}
}
This creates the cube at any point in the map I click, except for empty spaces, I tried using Raycast textureCoord but it always creates the cube at 0,0,0. I found on answers.unity3d.com that I had to use a mesh collider for the cube and that's what I did but it still doesn't work.
What do I have to do to make it work?
Sorry, I've read what you wrote a few times and I can see what you are trying to do, but I'm not clear on what part of it isn't working. Can you please clarify?
Thanks. When I press Fire1 (L$$anonymous$$B) it does create the cube at the point I want it to, but I want to create the cube on top of a cube I click using raycast texturecoord, but when using raycast texturecoord it always creates the cube at 0,0,0 (on top of the first cube) at not on top of the cube I click.
I tried this:
var newPosition = Vector3(hit.textureCoord.x, hit.textureCoord.y+1, 0); block = Instantiate (BlockPrefab, newPosition, Quaternion.identity);
Bare in $$anonymous$$d this is just a simple test for me to learn Unity3D.
Your answer
Follow this Question
Related Questions
Why in Unity3D RaycastHit. textureCoord always return 0,0 after building the project? 1 Answer
Sphercast hitinfo TextureCoord rounded? 1 Answer
Detect if object is a cube(find the primitive type) with physics.raycast 2 Answers
Painting on a texture without crossing mesh triangle. 1 Answer
How i can create a cube with specific coordenates? 3 Answers