- Home /
Instantiated Bullets Not Picking Up Correct Position/Rotation
I've been trawling through here and Google for the last three hours to no avail, so hoping I can get some help before I pull my hair out.
Recently instantiating the projectiles of my weapon has broken. I don't know when this occurred, so I can't retrace my steps.
The current projectile instantiation script is:
Projectile newProjectile = Instantiate(projectilePrefab, weaponMuzzle.position, weaponMuzzle.rotation);
The weaponMuzzle is an empty container on the weapon specifically to spawn projectiles. It was originally child to the barrel, but I've moved it to the parent layer for testing. No difference has been observed. The weapon itself sits in a container under the player object when equipped, and the container (and therefore everything under it) is rotated according to the camera view. I don't know if the fact that the weapon may not have its own rotation because it's a child may be affecting anything, but I'm certain it was the same way before and worked.
The bullet instantiates in front of the player, but always as if they're looking straight ahead (e.g. the bullet picks up the correct direction, but doesn't pick up if the gun is aimed up or down (as in it doesn't pick up the variations on the y axis) and doesn't pick up the aimed angle (so aiming up will produce a bullet that shoots straight forward).
That feels quite wordy so here's an amazing diagram:
I've used Quaternions and eulerAngles to no avail. I've also tried just stripping out the rotation aspect completely just to try and get the bullet to line up with the gunMuzzle, but I can't seem to get that to work, suggesting to me this isn't just a rotation issue.
I feel like I'm overlooking something really simple, can someone put me out of my misery?
Thanks in advance
EDIT: Troubleshooting I've tried
Instantiating at weaponMuzzle.parent.position produces the same result
Instantiating, and then setting the transform.position and transform.eulerAngles as a later step (to rule out anything else resetting the position) does not resolve
Setting the instantiated objects Quaternion to transform.eulerAngles.x/y/z does not seem to do anything
Instantiating as a child and then immediately setting the newProjectile.transform.parent to null does not resolve.
I passed through the players camera object and used that to set the position/rotation of the instantiated projectile as I suspected the child objects were not seeing this, but the same issue is occurring.
I feels to me like it can't get the proper information from the object it's instantiated from anymore?
EDIT2: Further steps tested:
Creating an instance as a child and then immediately removing the link gives same results. Code used:
Projectile newProjectile = Instantiate(projectilePrefab, weaponMuzzle.position, weaponMuzzle.rotation, weaponMuzzle.transform); newProjectile.transform.SetParent(null);
- Going back to the original script isn't working anymore for some reason. Original script on the weapon:
Vector3 shotDirection = GetShotDirectionWithinSpread(weaponMuzzle);
Projectile newProjectile = Instantiate(projectilePrefab, weaponMuzzle.position, Quaternion.LookRotation(shotDirection));
newProjectile.Shoot(this);
Corresponding code on the Projectile:
public void Shoot(WeaponController controller)
{
owner = controller.owner;
initialPosition = transform.position;
initialDirection = transform.forward;
initialDirection = transform.forward;
inheritedMuzzleVelocity = controller.muzzleWorldVelocity;
initialCharge = controller.currentCharge;
}
Also discovered that if the WeaponFX when shooting isn't childed to the weapon, it suffers from the same issue, so there must be a problem with my overall logic.
Still feel like I'm missing something basic. Done a lot of reading up about Instantiation and it all makes sense and I can get working, so I'm assuming something else is conflicting with this.
[1]: https://learn.unity.com/project/fps-template
As it's a projectile I haven't been spawning it with a parent, should I be?
When I was testing I did spawn it with the weaponMuzzle as the parent, which then picked up the correct location/angle but kept its movement with the gun after firing, so if the player turned so did the bullet
I haven't done the Unity FPS Microgame but my guess is that the bullet is coded to just move forward in what ever direction it is facing. It sound like you can get it to fire in what ever direction the gun in facing in a circle but not up or down. This means the bullet is simply not facing up or down correctly when it is initiated. I notice you have 2 lines in your Shoot function that say the exact same thing: initialDirection = transform.forward; Is that suppose to be that way?
I notice you have 2 lines in your Shoot function that say the exact same thing: initialDirection = transform.forward; Is that suppose to be that way?
As part of testing I've actually disabled everything in the bullet object, so I'm just concentrating on instantiating the bullet at the right position/rotation for now. The issue still persists, so I know it's nothing on the bullet script now at least.
It sound like you can get it to fire in what ever direction the gun in facing in a circle but not up or down.
That's correct, but also when the gun is pointed upwards the bullet doesn't instantiate higher/lower to take in the angled barrel. It's almost like it isn't understanding its own world rotation.
Is the empty weaponMuzzle still on the weapon? When you move your view up and down is the rotation of the weaponMuzzle changing in the editor?
Answer by JackhammerGaming · Feb 20, 2021 at 07:37 AM
try this @Bishmanrock
GameObject instantiated;
void Start(){
instantiated = Instantiate(projectilePrefab, weaponMuzzle.position, projectilePrefab.rotation); // this will instantiate the bullet as the same rotation set in your prefab
//only use this if your position still does not work properly
instantiated.transform.SetPositionAndRotation(weaponMuzzle.position , instantiated.transform.rotation); // if totation doesn't work here try quaternion.identity() function instead of instantiated.transform.rotation
}
I stripped everything back and gave this a shot and it worked, thank you!
Answer by VoidPhoenix96 · Feb 20, 2021 at 02:44 AM
Rotate the bullet spawn until it works.
Not sure what you mean? If I rotate the bullet spawn all it does is break the parts that are working, and makes the broken parts even more broken.
The bullet spawn is already facing the correct direction. The issue is related to the instantiated object not always picking up that direction. The bullet has also been confirmed to have been set to 0, 0, 0, so it's not a case of the direction of the prefab being off.