1
\$\begingroup\$

I am facing a problem below is the detail.

  1. There are two scripts(actually more than two but these have the problem) Cannon.cs and Launch.cs and three buttons(up, down, and launch) to move cannon and launch the bullet.
  2. Cannon have bullets and each bullet has Launch.cs attached also a RigidBody2D.
  3. When specific event occurs the Cannon.cs calls the Launch.cs's Launch(direction); method.
  4. Parameter "direction" = Cannon.transform.right.
  5. Bullets are being instantiated dynamically and are stored in the list Cannon.cs take one bullet and fires it(firing includes calling the Launch(direction));.
  6. Launch(direction); will always be called once no matter what also to double sure myself I destroy the launch script on launch.

The problem is once the bullet is fired or launched in the direction specified by the direction of cannon(since direction = cannon.right) and I click on the up or down button the launched bullet changes its direction which should never happen because Launch() method is called only once in the lifetime of the bullet and also I detach the Launch.cs script on launch.

I am using rigidbody2D.velocity = direction * thrust; what I think is that rigidbody2D.velocity once called will possess the direction just like a method called by reference. I am totally lost I hope I made my question clear please do help me if you can.

\$\endgroup\$
  • \$\begingroup\$ Are the bullets child-objects of the cannon? If yes they keep their relative position and direction when you change that of the parent-object. \$\endgroup\$ – Philipp Mar 12 '16 at 13:25
  • \$\begingroup\$ Oh thanks how can I miss it, so if on launch I set bullet's parent = null the issue will get solved? \$\endgroup\$ – Daniyal Azram Mar 12 '16 at 13:33
  • \$\begingroup\$ Issue solved!!! thank you very much, some time issues are not that big but our thoughts are I so grateful of you, it is like third hour fighting this issue and not even for a moment I thought of this parent child relationship:) \$\endgroup\$ – Daniyal Azram Mar 12 '16 at 13:36
1
\$\begingroup\$

Are the bullets child-objects of the cannon? If yes they keep their relative position and direction when you change that of the parent-object. Set transform.parent = null on the bullets to turn them into independent game objects.

\$\endgroup\$

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.