Hey All unity3D members im having a little bit of problems with my script. iv made a script that spawns objects and it works great the problem i am having is that they spawn halfway inside the other object when i click for them to be spawned how can i make it so the raycast hit makes it spawn the object on the side of the hit object? Thanks. Code (CSharp): Ray ray; RaycastHit hit; ray = Camera.main.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(ray, out hit, 100.0f)) { if(UC.BuildingModes == 0) { if(Env.BuildMode == true) { if (hit.collider.tag == "CanBuild") { try { if(UC.RotationModes == 1) { GameObject cube = (GameObject)Instantiate(ObjectsToSpawn[int.Parse((UC.BlockID))],new Vector3(hit.point.x,hit.point.y,hit.point.z), transform.rotation); cube.transform.parent = parentObject; } if(UC.RotationModes == 0) { GameObject cube = (GameObject)Instantiate(ObjectsToSpawn[int.Parse((UC.BlockID))],new Vector3(hit.point.x,hit.point.y,hit.point.z), hit.transform.rotation); cube.transform.parent = parentObject; } } catch { Debug.Log("ID Entered Is Invalid"); } } } }
You have to add half the size of the object you're spawning. As it is now, it spawns the pivot of the new object at hit. Best place to get the size: http://docs.unity3d.com/ScriptReference/Collider-bounds.html
Code (CSharp): newCollider.bounds.extents This is a Vector3 with half the size of the Collider in every dimension. You'll now have to take that and offset the object in the according direction, which will depend on where on the other object you clicked.
So this kind of works correctly but only for 1 axis i am a little confused now haha Code (CSharp): GameObject cube = (GameObject)Instantiate(ObjectsToSpawn[int.Parse((UC.BlockID))],hit.point + test.collider.bounds.extents, transform.rotation);
Ok so i now have this code and it works great i can spawn a cube and align it exactly with the other side of the default start cube in the scene the problem i have now is that it cant build after it takes up all the sides Code (CSharp): GameObject cube = (GameObject)Instantiate(ObjectsToSpawn[int.Parse((UC.BlockID))],hit.point, hit.transform.rotation); cube.transform.position = hit.transform.position + hit.normal; cube.transform.parent = parentObject;