2
\$\begingroup\$

I'm working on a 3D game in Unity and I'm having a problem trying to spawn an object exactly in front of a wall in my game, like hanging a picture on it. I don't want the object to look like it's too far from the wall or overlapping the wall.

public void SpawnPrefab(Transform prefab)
{
    compSize = prefab.gameObject.GetComponent<Collider>().bounds.size.z;
    Vector3 mousePosition, targetPosition;
    InputManager inputManager;
    inputManager = prefab.gameObject.GetComponent<InputManager>();
    mousePosition = Input.mousePosition;
    targetPosition = Camera.main.ScreenToWorldPoint(new Vector3(mousePosition.x,mousePosition.y,mousePosition.z + compSize/2 + wallSize/2));
    prefab.localPosition = targetPosition;
    if(isAlreadyClicked == false)
    {
        GameObject comp = (GameObject) Instantiate(prefab.gameObject, new Vector3(prefab.transform.position.x, prefab.transform.position.y, prefab.transform.position.z + compSize/2 + wallSize /2), prefab.transform.rotation);
        isAlreadyClicked = true;
    }
}

I tried adding float value on the mouseposition.z like mouseposition.z + <somefloatvalue> but it doesn't seem working like how I wanted because I have different object with different sizes, where some objects spawn at the back of the wall, while some will spawn too far at the front from the wall.

Update: I managed to get the accurate z position for the spawning process, but there's something wrong with its x and y position, it's not spawning based on the mouseclick position.

public void SpawnPrefab(Transform prefab)
{
    clicked = false;
    compSize = prefab.gameObject.GetComponent<Collider>().bounds.size.z;
    Vector3 mousePosition, targetPosition;
    InputManager inputManager;
    inputManager = prefab.gameObject.GetComponent<InputManager>();
    mousePosition = Input.mousePosition;
    targetPosition = Camera.main.ScreenToWorldPoint(new Vector3(mousePosition.x,mousePosition.y,mousePosition.z));
    ray = Camera.main.ScreenPointToRay(targetPosition);
    float rayDistance;
    if(plane.Raycast(ray, out rayDistance))
    {
        prefab.localPosition = ray.GetPoint(rayDistance);
        Debug.Log("clicked");
        if(isAlreadyClicked == false)
        {
            OnClickedButton();
            GameObject comp = (GameObject) Instantiate(prefab.gameObject, new Vector3(targetPosition.x, targetPosition.y, frontWall.gameObject.transform.position.z + compSize - wallSize), prefab.transform.rotation);
            isAlreadyClicked = true;
            Debug.Log(clicked);
        }
    }
}

I don't know what is happening...

\$\endgroup\$
  • \$\begingroup\$ You want your object to spawn where you click on the wall ? \$\endgroup\$ – Shashimee Aug 1 '17 at 7:07
  • \$\begingroup\$ No, I have a button, and I spawn the object according to the button position, by means when I click the button, it will spawn at the same position as the button (mouseclick) position, and I have a wall where I hope the object will not spawn at the back of the wall. \$\endgroup\$ – Mira Aug 1 '17 at 7:30
1
\$\begingroup\$

This example might be what you are looking for :

Ray myRay;      // initializing the ray
RaycastHit hit; // initializing the raycasthit
public GameObject objectToinstantiate;

void Update ()
{
    myRay = Camera.main.ScreenPointToRay (Input.mousePosition); // telling my ray variable that the ray will go from the center of 
// my main camera to my mouse (This will give me a direction)

     if (Physics.Raycast (myRay, out hit)) { // here I ask : if myRay hits something, store all the info you can find in the raycasthit varible.
// things like the position where the hit happend, the name of the object that got hit etc..etc..

         if (Input.GetMouseButtonDown (0)) {// what to do if i press the left mouse button
             Instantiate (objectToinstantiate, hit.point, Quaternion.identity);// instatiate a prefab on the position where the ray hits the floor.
             Debug.Log (hit.point);// debugs the vector3 of the position where I clicked
         }// end upMousebutton
     }// end physics.raycast    
}// end Update method

Source : this Unity question.

This looks a lot simpler than what you were doing and using this you will make sure that the coordinates of the prefab are exactly where you want them to be. You will just have to adjust the Z axis as you did before.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.