0
\$\begingroup\$

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 and shoot the projectile, I instantiate the projectile, copy the projectile spawn position to the projectile'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 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 moving and creating a projectile looks like this:

void FixedUpdate()
{
    
    if (Input.GetKey(KeyCode.W))
    {
        //player.transform.Translate(Vector3.forward * 10f * Time.deltaTime);
        this.GetComponent<Rigidbody>().AddForce(transform.forward * 15);
    }
    if (Input.GetKey(KeyCode.S))
    {
        this.GetComponent<Rigidbody>().AddForce(transform.forward * -15);
    }
    if (Input.GetKey(KeyCode.A))
    {
        transform.Rotate(Vector3.up * -15 * Time.deltaTime);
    }
    if (Input.GetKey(KeyCode.D))
    {
        transform.Rotate(Vector3.up * 15 * Time.deltaTime);
    }
}

void ShootBullet()
{
    //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<Rigidbody>().AddForce(bulletSpawn.forward * bulletSpeed, ForceMode.Impulse);
    
    //destroy bullet after x time 
    Destroy(bullet, lifeTime); 
}
\$\endgroup\$
8
  • \$\begingroup\$ Try to clarify what you mean by straight forward. Forward relative to what? Also clarify what is going wrong \$\endgroup\$ – Adam B Dec 14 '20 at 17:19
  • \$\begingroup\$ Let me see if I can describe it, if I move the parent object forward by adding force then turn left by yawing and fire a projectile, it's expected the projectile will will appear to be turning right since we're moving away from it's forward path of travel but with this code, the projectile will actually turn left and lead the parent, almost like it's using the parent's trajectory of momentum. Does that make sense? \$\endgroup\$ – HongCruise Dec 14 '20 at 17:43
  • \$\begingroup\$ Is this happening after it’s shot? Or at the time of shooting. Perhaps it’s a child of the gun? So when the gun rotated the child gets moved along with it. \$\endgroup\$ – Adam B Dec 14 '20 at 17:48
  • \$\begingroup\$ I checked the hierachy menu and it shows that it's not a child of the parent when I used this: bullet = Instantiate(bulletPrefab); I should add, if I constantly shoot the bullet as it's displaying this strange behavior, it eventually straightens out (after I release the turn button), but I also notice this is when the parent stops drifting from the turn. Again, not sure if that makes sense. It's almost like copying the rotation of the bulletSpawn transform ALSO copies it's velocity/momentum somehow. \$\endgroup\$ – HongCruise Dec 14 '20 at 19:16
  • \$\begingroup\$ Maybe show the code where you’re calling the spawn method. Also make sure the bullet doesn’t have the fixedupdate script in it. \$\endgroup\$ – Adam B Dec 14 '20 at 20:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.