• 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
Question by carlfarra · May 06, 2014 at 08:54 AM · javascriptmovementrigidbody2daddforceasteroids

Not understanding how rigidbody2D.AddForce works

I know t$$anonymous$$s has been asked before but I'm not able to find any answer that's not too technical or vague for me (I'm really new to coding). I'm creating an Asteroids-like space shooter using Unity's 2D features, but I don't know how to make rigidbody2D.AddForce work to make the s$$anonymous$$p move forward using the W key. A clear "try t$$anonymous$$s code" answer with an explanation would be tremendously appreciated. I had made the game in 3D and it works fine with t$$anonymous$$s code:


   #pragma strict
     
     var thrust: int;
     var rotationSpeed: float;
     var thrustEmitter: ParticleEmitter;
     
     function Update () {
     
         if (Input.GetKey (KeyCode.A))
         transform.Rotate(Vector3.left * Time.deltaTime * rotationSpeed);
         if (Input.GetKey (KeyCode.D))
         transform.Rotate(Vector3.right * Time.deltaTime * rotationSpeed);
         
         if (Input.GetKey (KeyCode.W)){
             thrustEmitter.emit=true;
             rigidbody.AddForce(transform.forward*Time.deltaTime*thrust);
                 }
              
         else{
             thrustEmitter.emit=false;
             }
     }


When remaking the game in 2D, the rotation and particle emitter are working properly, but the s$$anonymous$$p's not moving forward. Here's the code I have right now for 2D (Thrust is set to 1000):

 #pragma strict
 
 var thrust: int;
 var rotationSpeed: float;
 var thrustEmitter: ParticleEmitter;
 
 function Update () {
 
     if (Input.GetKey (KeyCode.A))
     transform.Rotate(Vector3.forward * Time.deltaTime * rotationSpeed);
     if (Input.GetKey (KeyCode.D))
     transform.Rotate(Vector3.back * Time.deltaTime * rotationSpeed);
     
     if (Input.GetKey (KeyCode.W)){
         thrustEmitter.emit=true;
         rigidbody2D.AddForce(Vector2.up * thrust);
     }         
     else{
         thrustEmitter.emit=false;
         }    
 }



Thanks!

Comment

People who like this

0 Show 3
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 robertbu · May 06, 2014 at 09:47 AM 0
Share

I don't see any problem here. The top of your sprite is what you consider the 'forward' side? Is 'thrust' set to a high enough value in the Inspector? Is it not moving or moving in the wrong direction.

avatar image carlfarra · May 06, 2014 at 10:13 AM 0
Share

Hey robertbu. "forward" and "back" are the left and right of my sprite, respectively. 'thrust' is set at 1000 right now, setting it to 10000 or 100000 isn't changing anything. Right now the sprite isn't moving at all, only rotating with the A and D keys.

avatar image LeonardNS · Aug 08, 2014 at 06:32 PM 1
Share

Just for debugings sake try to switch "Vector2.up" with "Vector3.one" or "new Vector3(1,1,1)" and check if it moves at all. If you have rotated your rocket in a wierd way the y axis may be in the depth and you are only moving the rocket away from the camera, but since it's a 2D game you won't see that. At this point though it should not move in the right direction.

0 Replies

· Add your reply
  • Sort: 

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

21 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

Related Questions

Rigidybody2D addforce at constant speed 1 Answer

move 2d character affected by physics with velocity and/or add force 2 Answers

Dashing with rigidbody2D not working right 2 Answers

Can someone help me with this Rigidbody2D bug? 1 Answer

Best way to move a Rigidbody2D from point A to point B (Mouse Position) 3 Answers


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