Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
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.

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

Issue while walking on multiple box collider 2D 1 Answer

Adding Foward Force to A Rigidbody2D 0 Answers

Rotate object based on projectile impact 0 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