• 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
1
Question by lepickle · Sep 05, 2014 at 02:10 PM · rigidbody2dphysics2dunity4.3asteroids

How to create an Asteroids style moving in 2D mode?

I'm working on an asteroids style game, and I can't figure out how to apply a forward velocity for the ship. So far I've done is a rotation but any form of forward thrust I can't do.

This is my code so far:

     public float speed = 10.0f;
     public float rotationSpeed = 10.0f;
     // Update is called once per frame
     void Update()
     {
         if (Input.GetKey(KeyCode.W))
         {
             //not working
             rigidbody2D.AddForce(Vector2.up * speed);
         }
         float rotate = Input.GetAxis("Horizontal") * rotationSpeed;
         transform.Rotate(0, 0, 0 - rotate);
     }
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

2 Replies

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

Answer by marsfan · Sep 05, 2014 at 03:18 PM

For a start, use rigidbody2d.addrelativeforce as shown here: docs.unity3d.com/ScriptReference/Rigidbody2D.AddRelativeForce.html This will add force along the local axis. This will make setting the direction of force easier.

Comment
Add comment · Show 3 · 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 lepickle · Sep 06, 2014 at 08:12 AM 0
Share

There's a compiler error saying:

Error 8 'UnityEngine.Rigidbody2D' does not contain a definition for 'AddRelativeForce' and no extension method 'AddRelativeForce' accepting a first argument of type 'UnityEngine.Rigidbody2D' could be found (are you missing a using directive or an assembly reference?) C:\Users\$$anonymous$$ark\Documents\Unity Projects\Swingship\Assets\playerScript.cs 87 25 Assembly-CSharp-vs

I've also tried Rigidbody2D.AddRelativeForce (With capital R). But nothing comes up in the VS intellisense.

avatar image sevensixtytwo · Sep 06, 2014 at 10:47 AM 0
Share

How exactly did you call the AddRelativeForce? IIRC, it accepts a Vector2 plus the Force$$anonymous$$ode so it should look something like this:

 this.rigidbody2D.AddRelativeForce(Vector2(x,y),Force$$anonymous$$ode2D.Force);

Since it's relative it will add x force along the object's local x-axis and y force on the local y-axis.

avatar image lepickle · Sep 07, 2014 at 01:56 PM 0
Share

Sorry, for the late response. it seems that I have to use transform.up ins$$anonymous$$d of Vector2.up. Now the sprite is moving around. Thank you for answering, though. $$anonymous$$y new code is:

 rigidbody2D.AddForce(transform.up * Time.deltaTime * speed * Input.GetAxis("Vertical") * 0.5f);
avatar image
0

Answer by Multeezee · Aug 22, 2019 at 08:09 PM

This helped me find this: https://docs.unity3d.com/ScriptReference/Rigidbody2D-drag.html which helped me slow down a RigidBody Underwater. Thank you!

Comment
Add comment · 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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

rigidbody2D.MovePosition() Can't Found 2 Answers

Unity2d Set Different Physics Properties on Gameobjects (potentially at Runtime) 2 Answers

Reset Rigidbody2D rotation after collision 2 Answers

Adding Collider2D changed physics on race car.How to compensate for mass loss in the back of the car?or do it some other way. 0 Answers

Unity 2D Physics .AddForce 1 Answer

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