- Home /
Bullet not spawning where it should be,Bullet spawning away from the prefab
Hey guys, new to coding so I will be quick
I got a pistol prefab and a bullet prefab. I got my gun prefab to instantiate onto my player model as a child of the hand no problem, however, whenever I shoot my bullets it does not come out where I have the spawner (Which is a child of the pistol prefab) but instead the area the spawner was before I made the pistol prefab and destroyed the original pistol. No matter where I move or how I turn, it will always spawn in the same area going in the same direction. Here is the code for the shooting on the actual pistol:
using UnityEngine;
using System.Collections;
public class pistol_controls : MonoBehaviour {
public GameObject bullet_fixed;
public Transform pistol_spawner;
public void fire()
{
var bullet = (GameObject)Instantiate(bullet_fixed, pistol_spawner.transform.position, pistol_spawner.transform.rotation);
Rigidbody temp_rigidbody;
temp_rigidbody = bullet.GetComponent<Rigidbody>();
temp_rigidbody.AddForce(transform.forward * 20);
}
}
Anything I did wrong?
UPDATE: An example of the exact problem I am having
So locally to the pistol, say that the spawner is 10, 10, 10 (As an example). The bullets spawn globally at 10, 10, 10 every single time. If I change the local position of the spawner compared to the pistol to 5, 5, 5 the bullets spawn at 5, 5, 5 globally
I'm sorry, but I have to ask. Is the spawner a child of the original pistol or a child of the instantiated pistol?
You code looks correct to me at first glace, so having the wrong pistol_spawner reference would account for your description of the issue.
The spawner is a child of the original pistol which I then made the prefab out of
Can you post a screenshot of your hierarchy so that we can recreate your setup? As written, you should be spawning things at their global positions as you're using global positions as arguments for the Instantiate call.
Is there anything else that might be influencing your bullet's position? Are you maybe moving the bullet in a weird way with a script?
I think the issue is the pistol_spawner variable being used as a transform. Basically when you dragged and dropped the pistol position it just took that place in world space and saved it as a variable. Try re-doing it with the actual whole pistol spawner object set to the pistol_spawner variable (even if the only component is a transform), and then access the transform like you did with pistol_spawner.transform.position (this will now give the local position of the game object relative to the parent, rather than a global transform in world space). public GameObject pistol_spawner ins$$anonymous$$d.
Not sure, Transform and GameObject are classes, making them reference types. All the inspector assignment does is tell Unity that this instance of pistol_controls
will start with a reference to whatever Transform instance OP assigned in the editor.
This would be the case if that was a Vector3 or some other value type, though.
After reading your update, this problem stopped making sense to me.
When you say locally to the pistol, do you mean as a child of the pistol? Because the code you are showing us, is the world-position of the pistol_spawner. For that to match up its local-position, the pistol_spawner would either need to be parentless, or its parent's local-space would need to match up to world-space. The reason I'm confused is because I'm reading your problem as if the pistol_spawner is a child of the pistol GameObject, but your description of how the coordinates are acting is making it sound like it isn't a child of the pistol GameObject.
I've checked while the game is running, the spawner is definitely a child of the pistol when the pistol itself is added to the game.
Answer by Razor_edge · Jul 13, 2018 at 05:43 AM
I FIGURED IT OUT!!!!!
The problem was that the character script was calling the firing function of the prefab and NOT the function of the instantiated gun, therefore it spawns where I placed the prefab before I put into my project files NOT where the instantiated gun is. I feel so stupid figuring this out but baby steps.
THANK YOU TO ANYONE THAT ASSISTED ME.
Answer by bner2008 · Jul 11, 2018 at 11:46 AM
Hello, I suggest you check if your prefabs default position/rotation values are set to 0, not the instance of the prefabs but the prefabs themselves. It's easy to forget that and that can lead to a lot of issues if you try to use their position because it won't be precisely where you'd think they'd be.
It wasn't set to 0, 0, 0... so thanks for re$$anonymous$$ding me that. However, it still isn't spawning where I want it. Thanks for the suggestion though.
Answer by Strixie13 · Jul 11, 2018 at 09:56 PM
I think the error is on the last line. It says "transform.forward" which would use the transform of the game object the entire script is attached to instead of the spawner object. Try temp_rigidbody.AddForce(pistol_spawner.forward);
No, sorry man, still spawning not where I need it. I should make a note that the bullets spawn in the exact same place in the global transformation and it is linked to the local translation of the spawner to the gun. It's odd. Thanks anyway.
Answer by kodierer · Jul 11, 2018 at 10:20 PM
Maybe try transform.localPosition transform.localRotation or even setting the local positions to 0 in code. transform.localPosition.x=0 etc..
As a hack, you may try seeing how far away things are spawning, and subtracting the difference.
Tried this and didn't work again. It isn't that the bullets spawn away from the spawner and changes globally when I move, I mean that the bullets spawn in the same place globally but using the local space the spawner is compared to the pistol, if that makes sense.
So locally to the pistol, say that the spawner is 10, 10, 10 (As an example). The bullets spawn globally at 10, 10, 10 every single time. Thanks for the recommendation though
Answer by AdityaViaxor · Jul 12, 2018 at 11:36 AM
Actually just comment the last line then see position because when it instantiate you forward it by 20 unit so try commenting it.
Thanks very much for the suggestion, though that's not the solution.
That line works only to propel the bullet forward after it instantiate, which works well, it is just spawning in the wrong position. I've narrowed the problem down:
The script for some reason is using the local transformations of the spawner object as the global transformations to use the bullet with, so like I said at the update, if the spawner was located 10, 10, 10 on the actual pistol itself, the bullets start spawning at 10, 10, 10 in the global scope of the scene.
Your answer
Follow this Question
Related Questions
Tower defense target update 0 Answers
Card Game like yu-gi-oh 1 Answer
rb.AddForce (transform.forward * BulletForce) has issues. 1 Answer
Muzzle Flash Prefab 1 Answer