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.