Search Unity

c# Spawning Objects Correctly?

Discussion in 'Scripting' started by mumbot, Apr 17, 2015.

  1. mumbot

    mumbot

    Joined:
    Oct 19, 2012
    Posts:
    86
    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):
    1. Ray ray;
    2.             RaycastHit hit;
    3.             ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    4.             if (Physics.Raycast(ray, out hit, 100.0f))
    5.             {
    6.                 if(UC.BuildingModes == 0)
    7.                 {
    8.                     if(Env.BuildMode == true)
    9.                     {
    10.                         if (hit.collider.tag == "CanBuild")
    11.                         {
    12.                             try
    13.                             {
    14.                                 if(UC.RotationModes == 1)
    15.                                 {
    16.                                     GameObject cube = (GameObject)Instantiate(ObjectsToSpawn[int.Parse((UC.BlockID))],new Vector3(hit.point.x,hit.point.y,hit.point.z), transform.rotation);
    17.                                     cube.transform.parent = parentObject;
    18.                                 }
    19.                                 if(UC.RotationModes == 0)
    20.                                 {
    21.                                     GameObject cube = (GameObject)Instantiate(ObjectsToSpawn[int.Parse((UC.BlockID))],new Vector3(hit.point.x,hit.point.y,hit.point.z), hit.transform.rotation);
    22.                                     cube.transform.parent = parentObject;
    23.                                 }
    24.                             }
    25.                             catch
    26.                             {
    27.                                 Debug.Log("ID Entered Is Invalid");
    28.                             }
    29.  
    30.                         }
    31.                     }
    32.                 }
     
  2. Codermic

    Codermic

    Joined:
    Feb 12, 2015
    Posts:
    7
  3. mumbot

    mumbot

    Joined:
    Oct 19, 2012
    Posts:
    86
    iv never used this before could you by any chance provide me of a example?
     
  4. Codermic

    Codermic

    Joined:
    Feb 12, 2015
    Posts:
    7
    Code (CSharp):
    1. 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.
     
  5. mumbot

    mumbot

    Joined:
    Oct 19, 2012
    Posts:
    86
    So this kind of works correctly but only for 1 axis i am a little confused now haha
    Code (CSharp):
    1. GameObject cube = (GameObject)Instantiate(ObjectsToSpawn[int.Parse((UC.BlockID))],hit.point + test.collider.bounds.extents, transform.rotation);
     
  6. mumbot

    mumbot

    Joined:
    Oct 19, 2012
    Posts:
    86
    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):
    1.                     GameObject cube = (GameObject)Instantiate(ObjectsToSpawn[int.Parse((UC.BlockID))],hit.point, hit.transform.rotation);
    2.                     cube.transform.position = hit.transform.position + hit.normal;
    3.                     cube.transform.parent = parentObject;