shooting projectile without inheriting parent's momentum
Hi all, not sure if anyone has ever experienced this issue but I'm trying to shoot a projectile out of a moving "parent" object. Both objects move off of rigidbody physics so I'm using addforce for the projectile. Whenever I move the "parent" object and turn while it's moving then shoot the projectile, I instantiate the projectile, copy the projectile spawn position to the project's position, copy the rotation of the "parent" to the projectile then add force to the projectile. It'll shoot but I notice it moves in the direction of the momentum of the "parent" rather than straight forward from where it was created. I'm pretty sure this is just me not understanding how rigidbody physics works for adding force but if anyone has any ideas suggestions as to how to make the projectile move straight forward from where it's created rather than copying the momentum of the parent I'd appreciate it greatly. My code for creating a projectile looks like this:
//create bullet GameObject bullet = Instantiate(bulletPrefab);
//set location where bullet will spawn from bullet.transform.position = bulletSpawn.position;
//set direction of the bullet bullet.transform.rotation= bulletSpawn.transform.rotation;
//add force to the bullet bullet.GetComponent().AddForce(bulletSpawn.forward * bulletSpeed, ForceMode.Impulse);
//destroy bullet after x time Destroy(bullet, lifeTime);
Answer by HongCruise · Dec 18, 2020 at 05:25 PM
Sorry forgot to finalize my discovery or at least what I hope is the answer: GameObject bullet = GameObject.Instantiate(bulletPrefab, bulletSpawn.transform.position, bulletSpawn.transform.rotation);
bullet.GetComponent().velocity = (bullet.transform.forward * bulletSpeed) + this.GetComponent().velocity;
Destroy(bullet, bulletLifeTime);
I hope this helps others if they ever run into this issue.
Answer by xxmariofer · Dec 15, 2020 at 04:22 PM
Hello, adding force simply adds force in a direction so if it has a previous force it will not get override, you should remove the previous acceleration before adding the force
//create bullet GameObject bullet = Instantiate(bulletPrefab);
//set location where bullet will spawn from bullet.transform.position = bulletSpawn.position;
//set direction of the bullet bullet.transform.rotation= bulletSpawn.transform.rotation;
//remove forces
bullet.GetComponent<RigidBody>().velocity = Vector3.zero;
//add force to the bullet
bullet.GetComponent<RigidBody>().AddForce(bulletSpawn.forward * bulletSpeed, ForceMode.Impulse);
//destroy bullet after x time
Destroy(bullet, lifeTime);
Thanks for helping xxmariofer! I tried your code and unfortunately it's producing the same results but I was sure what you said was the case. From what I can tell applying the rotate of the bulletspawn for whatever reason is definitely carrying over the velocity strangely enough. It's even weirder that most tutorials online use this but I guess not many deal with no gravity travel at a higher speed while turning so they wouldn't notice this issue. After an exhausting research I think I somehow found a solution that does work: GameObject bullet = GameObject.Instantiate(bulletPrefab, bulletSpawn.transform.position, bulletSpawn.transform.rotation); bullet.GetComponent().velocity = (bullet.transform.forward * bulletSpeed) + this.GetComponent().velocity; Destroy(bullet, bulletLifeTime);
if you ask me why this works I can't explain it as I'm still trying to grasp how unity's rigidbody works
Your answer
Follow this Question
Related Questions
Bullet goes sideways when the player gets hit 1 Answer
projectile travels parallel to ground and not local space 1 Answer
How to spawn projectiles on the server from a client. 0 Answers
enemy projectiles 0 Answers
2.5D Projectile Help 1 Answer