public GameObject Bullet;
public Transform Spawner;
if(Input.GetMouseButton(1)) {
Instantiate(Bullet, Spawner.transform.position, Spawner.transform.rotation);
}
The spawner is on the front of the gun and its not in the scene its a prefab when i start it he places the weapon prefab with as child the Spawner and i walk a little bit the spawner goes with him but when i shoot he shoots at the floor at the position from where the charracter standing at the begin of the scene and there is not the spawner.
I see no issue in the code you posted. Are you 100% sure the spawner is moving and the transform that you have assigned to public Transform Spawner; is the same one that is supposed to shoot? It would be probably better if you posted more of your code if none of the above solves your problem.
First of all, you should call Input.GetKey in Update() “Note also that the Input flags are not reset until “Update()”, so its suggested you make all the Input Calls in the Update Loop.”
FixedUpdate is meant for physics updating, you should put there only physics related stuff (Raycast is not
necessarily
physics related in this case)
You also shouldn’t call GetComponent each frame, it’s a waste of resource, just call it in Awake or Start function and move the variables outside of the function.
For the spawning problem, there must be something wrong with Spawner.transform.position… Try to Debug.Log it. If you have script on the bullet, check if it isn’t tinkering with transform.position.
The things I wrote before were just issues I found, that could cause some issues in the future.
In the hierachy there creates a spawner when i hit play because i have a humanoid with a gun prefab in his self and when i hit play you can see the weapon and in the weapon prefab is a child named Spawner and thats an empty gameobject. I set it into the player gameobject and transform.
Maybe try the spawned as a public GameObject instead of public Transform. I do it that way & the only issue I have is if I forget to line the axis up correctly.
I’m no expert with setting it up using the drag and drop objects in, I tend to do it through code, but just a few suggestions. The Spawner variable is on your character, yet is parented under the gun object. It would make things easier to have that variable (and probably the rest of the script) on the gun. At the moment it looks like you are attaching the ‘original’ spawner object as the transform so when the gun is instantiated (think cloned) it can’t attach it’s own, cloned, spawner. Imagine if you had 2 guns for some reason - how would it know which of the clones to use?
To do it without using scripting to find the object at runtime - drop a copy of your weapon into the scene parented under the Rig Pistol Right. Then drag its Spawner object onto your script. Disable this weapon gameobject in the inspector. Then in the script that makes your gun, instead of instantiating the gun, just enable the gameobject.
You could do this for each type of gun, so instead of creating them as they are collected/chosen/whatever you just turn them on and off. For all it to really work properly, you want to move the spawner variable and script off the character and onto the guns themselves. The character shouldn’t know what happens when they “use their hands” (press mouse button 1). It should just call a “Use” script on the object in their hands. This may be fire a bullet, swing a knife, pet a kitten. It doesn’t matter.
Its also a good idea to rotate the Spawner object so that the z axis is pointing forwards for the bullets direction of travel.
I have just started programming with C#, and have not worked with that many children, so I am not sure this solution will work.
Your problem is that the Spawner object is the child of an other object, in the above case the “AutomaticRifle 1”. In the script you set it as a transform, but the game engine can not make a connection between the two. So you have to do that manually.
The first thing you need to do, as has been suggested, is set “Transform Spawner” to “GameObject spawner”. Now, before you instantiate the bullet, you need to refer the engine to your game object. Since it’s a child of the object “Soldier”, let’s start there:
If the “Soldier” is the one the script is running on, you can use “this” instead of “GameObject.Find(“Soldier”)”.
Like I wrote, I am not sure whether this will search the whole child tree, or just one level deep, it should however put you on the right track. I have done something similar with space stations and ships docking and undocking at empty game object children of the station.
One more thing: Since the find call is heavy on the CPU, and in the above example would be done every time a bullet is fired, the best place to put this line would be at the coroutine used to equip the gun.