• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by shamblinwithshotguns · Dec 15, 2017 at 06:09 PM · instantiaterigidbody2daddforce

Trying to move an instantiated object with a Rigidbody2D and AddForce

I am setting up a gameobject that uses a coroutine to endlessly spawn bullets, and I want to make it so that when I rotate the gameobject it also changes the direction the bullets are spawning and moving at. Currently, the projectiles do not move at all.

The first if statement is determined by a public bool laser, which I have toggled in the editor for specific prefabs. I have declared _bullet and _laser as GameObjects and serialized them so I can plug them in the editor. I have also declared a public RigidBody rb but haven't assigned anything to it in the editor. The Rigidbodies are kinematic.

 IEnumerator Shoot()
 {
     while (true)
     {
         yield return new WaitForSeconds(1);
         if (laser == false)
         {
             Instantiate(_bullet, transform.position + new Vector3(0, 0, 0), Quaternion.identity);
             rb = _bullet.GetComponent<Rigidbody2D>();
             rb.AddForce(new Vector2(10, 0));
         }
         else if (laser == true)
         {
             Instantiate(_laser, transform.position + new Vector3(0, 0, 0), Quaternion.identity);
             rb = _laser.GetComponent<Rigidbody2D>();
             rb.AddForce(new Vector2(10, 0));
         }
     }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by pako · Dec 15, 2017 at 07:37 PM

 Rigidbody rb;
 GameObject go;
 
  IEnumerator Shoot()
  {
      while (true)
      {
          yield return new WaitForSeconds(1);
          if (laser == false)
          {
              go = Instantiate(_bullet, transform.position + new Vector3(0, 0, 0), Quaternion.identity) as GameObject; //you must get a reference to the newly instantiated GameObject
              rb = go.GetComponent<Rigidbody2D>(); //then, get a reference to the Rigidbody of the newly instantiated GameObject (not the one on the prefab)
              rb.AddForce(new Vector2(10, 0));
          }
          else if (laser == true)
          {
              go = Instantiate(_laser, transform.position + new Vector3(0, 0, 0), Quaternion.identity) as GameObject;
              rb = go.GetComponent<Rigidbody2D>();
              rb.AddForce(new Vector2(10, 0));
          }
      }
  }


EDIT: Your Rigidbodies must not be kinematic if you want to move them using the Physics Engine, e.g. with AddForce().

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image shamblinwithshotguns · Dec 17, 2017 at 03:03 AM 0
Share

Thank you very much for clearing that up! I also had to change Quaternion.identity to transform.rotation. The turrets work as intended!

avatar image pako shamblinwithshotguns · Dec 17, 2017 at 10:10 AM 0
Share

I'm glad it now works for you. Could you please mark my answer as "accepted"? (Click on the green "check mark" ).

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

92 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Shoot logic, bullet instantiation 1 Answer

Can't add AddForce or velocity to RigidBody2D 1 Answer

Instantiated Bullet Force not being applied 0 Answers

Character instantly jumps instead of smoothly gaining altitude 3 Answers

Raycast 2D 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges