Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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.

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

Object after instantiate doesn't apply AddForce 1 Answer

How do I make an Instantiated Projectile travel towards Center of Screen 1 Answer

Acceleration and Max Speed on 2D Object 0 Answers

Use addForce to Jump 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges