- Home /
The question is answered, right answer was accepted
How to spawn GameObject exactly in front of another object?
Im 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 + 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.
Any help would be appreciated.
Thanks thanks thanks!
You may want to look at Decals, so that your object is exactly at the position of the wall, yet appears on top. This is the technique for bullet holes and other impacts.
Answer by miramaslow · Aug 03, 2017 at 08:32 AM
Thanks for the replies, but I managed to get it done. Here's the full code
public void SpawnPrefab(Transform prefab)
{
InputManager inputManager;
clicked = false;
compSize = prefab.gameObject.GetComponent<Collider>().bounds.size.z;
inputManager = prefab.gameObject.GetComponent<InputManager>();
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, out hit))
{
Debug.Log("clicked");
if(isAlreadyClicked == false)
{
OnClickedButton();
Instantiate(prefab, new Vector3(hit.point.x, hit.point.y, frontWall.gameObject.transform.position.z + compSize*2 - wallSize*3), prefab.transform.rotation);
isAlreadyClicked = true;
Debug.Log(clicked);
}
}
}
This is much more simpler solutions than what I did before. Source: this question , and this Unity question
Answer by Alturis2 · Aug 03, 2017 at 01:38 AM
You are going to have to take your ScreenToWorldPosition() result and then run a RayCast collision trace out from that point forward and place your object at the resulting hit position minus the bounds of the object. Sounds like you will also want to use the resulting surface normal to define the rotation of the object so it aligns to it.
https://docs.unity3d.com/ScriptReference/Physics.Raycast.html
I'm using a button to spawn the object according to the mouseclick position on the screen, do I have to use plane raycast ins$$anonymous$$d? since my origin point is the button..
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;
Input$$anonymous$$anager input$$anonymous$$anager;
input$$anonymous$$anager = prefab.gameObject.GetComponent<Input$$anonymous$$anager>();
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..
Answer by a161803398874 · Aug 03, 2017 at 05:00 AM
You can add an Empty GameObject and place it exactly where you want to spawn the objectc, we do this to keep track of the transform. In your script you add
public Transform emptyObjectTransform;
to get hold of the values, you will need to drag and drop the empty object to the script in the inspector, no when you pass the spawn values in your "Instantiate" method you pass
emptyObjectTransform.position
You can also pass rotation....
Dont recomend this method if you are going to spawn many items because you will need one emptygame object for each spawn pont
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Getting other cameras coordinates 1 Answer
Random position in rectangle, but not in a camera view 1 Answer
UNITY 3D: How to make the camera follow the player? Smoothly 2 Answers