- Home /
Spawning a Bullet at The edge of a Cube not the Center
So I have a cube that spawns spheres that travel 'along' a blue line. (I say travel because they actually do not interact. The flight path of the sphere and the blue line are computed completely separately)
And currently, the spheres are spawned at the center of the cube and fly out, however, enemy spheres - that can collide and destroy the ones you create - are colliding with yours inside the cube, which looks confusing because to the user sees nothing happening.
[You are the blue cube, the yellow sphere is what I was talking about they 'fly' down the bright blue line, but enemy spheres also come at you on the same line, thus my problem]
The only idea I had was to destroy the enemy spheres on the surface of the cube using colliders but I don't think that would solve my issue as your spheres would still be spawned inside the blue cube, and you still get a confusing animation if timed right, where you never see anything happening.
Is there a way to find out the collision point between the blue line and the blue cube, because I could use that as my spawn point, however, I don't know of a way to get those coordinates.
Thanks for any help you can give, sorry the vagueness, I don't know how else to describe the problem.
take one child object at edge into cube and spawn from that child
Answer by a161803398874 · Jul 29, 2017 at 05:45 AM
@PhantomOblivion hi there! First we need to save with a gameobject the position where you want to spawn the shpere to do this you add an empty game object to the scene and drop it inside your cube("the thing thats shooting") to parent it, now move this empty gameobject where you want to spawn the sphere. . Now in your code set a "public Transform myEmptyGameObject;" then set the "spawnpoint" in your script to this transform location. you will need to drag and drop the "emptygameobject" to the script in the inspector window for it to know what game object we´ve declared on top.
you might need to pass the value of the spawn point as a Vector3 since we´ve declared the name as "myEmptyGameObject" we do it like this. Vector3 newSpawnPos = new Vector3 (myEmptyGameObject.position);
goin to make the code to change position when you click mouse left btn ++++++++++++++++++++++++++++++++++++++++++++++++++++++
public Transform bullet;
public Transform myEmptyGameObject;
Vector3 newSpawnPos = new Vector3 (myEmptyGameObject.position);
if(Input.GetButton("Fire1")
{
bullet.position = newSpawnPos;
}
Answer by PhantomOblivion · Jul 29, 2017 at 02:47 PM
@a161803398874 thanks for the reply, however, I am already doing that. I have an empty game object called fire point as a child of the cube. The issue I can fire in any direction.
[The red cube that says 6 is attacking the blue cube and the blue cube is attacking the red cube that says 9]
[This is the Instantiate line I use to spawn the spheres - or as I call them energy Pulses - from the empty game object firePoint]
@vir1234 What I think I am going to do is create four firing points -one for each side of the cube- and then just draw to and spawn from them. I think that system will work later in the game once I start cleaning up the graphics and it will work for now while I am prototyping out the mechanics.
Your answer
![](https://koobas.hobune.stream/wayback/20220612131433im_/https://answers.unity.com/themes/thub/images/avi.jpg)