projectile travels parallel to ground and not local space
I am instantiating a cannonball from the end of my canon but can't figure out how to make the canon ball come out LOCAL to the canon barrel. They come out and always travel in world space forward parallel to the ground even when the canon is aiming up .I know it has to do with the "transform.forward" so what should it say? EDIT: the canon barrel moves up and down so adding a transform.up helps if it was always at that 45 degree angle. The addForce should be applied in the to coincide with the angle of the barrel
GameObject bullet = Instantiate(projectile, Muzzle.position,Quaternion. identity)as GameObject;
bullet.GetComponent<Rigidbody>().AddForce (transform.forward *throwDistance);
Answer by Hamburgert · Jan 23, 2018 at 06:29 AM
In your Instantiate, your third parameter is Quaternion.identity, which means "no rotation". See https://docs.unity3d.com/ScriptReference/Quaternion-identity.html
Your next line of code adds force in the bullets forward direction, namely the "no rotation" direction. This is ok, but you should first rotate the bullet to match the rotation of the cannons barrel. E.g:
bullet.eulerAngles = cannonTransform.Find("BarrelTransform").eulerAngles;
// Assuming the barrel transform and bullet transform has the same "forward" direction
Thank you so much! I guess I never noticed that because that code works for my rocket shooting robots but that is because they are shooting parallel to the ground. I wan able just to remove that "Quaternion" bit and works like I imagined it should
GameObject bullet = Instantiate(projectile, $$anonymous$$uzzle.position,transform.rotation)as GameObject;
bullet.GetComponent<Rigidbody>().AddForce (transform.forward *throwDistance);