- Home /
Issue with Instantiate and the instant that spawns on a moving object that rotates
This may be a very confusing question but I am having trouble with bullets spawning. the game is in 2D and when the player is moving the instant spawns left or right based on how it is moving I took a picture of it and ill post my bullet spawning code as well. I use C#.
here is the code, it doesn't have any error, but if the spawning has something to do with the movement here it is.
if ((Input.GetKey(KeyCode.Mouse0) || (Input.GetKey(KeyCode.F))) && bulletAmount > 0 && timePerShot == 0 && transform.localScale.y != 1 && gun == true)
{
Rigidbody bulletInstance;
var pos = Input.mousePosition;
pos.z = transform.position.z - Camera.main.transform.position.z;
pos = Camera.main.ScreenToWorldPoint(pos);
var q = Quaternion.FromToRotation(Vector3.up, pos - transform.position);
bulletInstance = Instantiate(bullet, gunBarrel.position, q) as Rigidbody;
bulletInstance.AddForce(gunBarrel.right * speed);
bulletAmount--;
timePerShot += timeBetweenShots;
audioSource.PlayOneShot(bulletSound, 2);
}
Answer by Buzzsaw5000 · Apr 22, 2016 at 04:21 PM
I have found one of the issues and fixed it. it turns out the gun cannot be a child of the player because they aren't the same size. now I have a new issue where I need the gun to equal the position of the player without shaking. I have tried fixed update and update but they aren't fast enough to keep up with the player. so either I have it on the player and have issues with the firing. or fix the firing and have an even worse jittering gun.