- Home /
Question by
Bangel1423 · Sep 06, 2012 at 05:48 AM ·
2dmouseshooter
Cannonball not shooting according to the rotation of the cannon.
Hello, im working on a small assessment piece and am having trouble with getting my cannon to shoot the cannon ball.
I have the cannon (very basic model) and a cannonball prefab (Rectangle) and i have made the cannonball shoot according to an empty gameobject that i have attached to the cannon via a fixed joint.
and it shoots fine. But i cant seem to figure out how to it to shoot according to its rotation.
Heres the code ive got.
Thanks for any help in advance.
public class Shooting : MonoBehaviour
{
public Rigidbody thePrefab;
public float speed = 5.0F;
void Update ()
{
if(Input.GetButtonUp("Jump"))
{
Rigidbody instance = Instantiate(thePrefab, transform.position, transform.rotation)as Rigidbody;
instance.AddForce(new Vector3(500 * speed,0,0));
}
}
}
Comment
Answer by ScroodgeM · Sep 06, 2012 at 06:54 AM
replace
instance.AddForce(new Vector3(500 * speed,0,0));
with
instance.AddForce(500 * transform.forward);
i hope your cannon should fire in it's forward (Z-axis) direction 8)
Awesome, thanks very much. I had to change .forward to .right, But worked great, thank you!