Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Slateboard · Jan 05, 2014 at 07:31 AM · unity 2d

How do I make a 2D object move in the direction it's facing, like .forward for 3D?

I hope I asked it correctly. Basically, I have a player ship, and I want it to move "forward", by pressing Up on the Keyboard, with left and right being for rotation.

Comment
Add comment · Show 2
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 ncallaway · Jan 05, 2014 at 06:39 PM 0
Share

When you say it "doesn't work", do you just mean that you see no movement?

If so, you'll need to deter$$anonymous$$e whether the problem is in the setup of your rigidbody (such that "rigidbody2D.AddForce()" doesn't do anything), or with your velocity calculation (such that "transform.forward boostSpeed 10" returns a zero vector, or otherwise incorrect vector)?

To decide whether it's the rigidbody or the velocity calculation try replacing line 30 ("rigidbody2D.AddForce(transform.forward boostSpeed 10)") with:

transform.Translate(transform.forward boostSpeed 10 * Time.smoothDeltaTime)

avatar image Slateboard · Jan 10, 2014 at 02:38 AM 0
Share

I tried this and it made the ship disappear inexplicably.

4 Replies

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

Answer by robertbu · Jan 10, 2014 at 04:39 AM

for Unity2D objects, use

 transform.right

rather than

 transform.forward

A 2D object moves on the XY plane. For a 2D object, transform.forward will be into the screen and therefore not have any meaning to a rigidbody2D.AddForce() call.

Depending on how you have your app setup, you will likely need either transform.up or transform.right or perhaps -transform.right in your AddForce() call.

Note that if you are using Translate , that anyway uses local space (by default) - so it is automatic.

Comment
Add comment · Show 1 · 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 Slateboard · Jan 10, 2014 at 03:18 PM 0
Share

I thought I had done that already but it appears that I did not. I've done what you and SuperLuigi have suggested and it works correctly.

Thanks.

avatar image
2

Answer by Mordengamer · Oct 01, 2017 at 09:44 AM

the easy way is to do transform.up instead of transform.forward its worked for me alt text


screenshot-2.png (3.3 kB)
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
avatar image
2

Answer by avip805 · Sep 06, 2018 at 10:08 PM

I know this post is really old, but that has not worked for me.

Comment
Add comment · Show 1 · 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 ziyadedris · May 09 at 08:08 PM 0
Share

-transform.up worked for me I was very close to giving up until I remembered that I forgot to try it.
I also noticed that transform.Translate(-transform.up * speed * Time.deltaTime); didn't work But transform.position += -transform.up * speed * Time.deltaTime; worked. Very confusing, anyone has any ideas?

avatar image
1

Answer by superluigi · Jan 10, 2014 at 05:12 AM

In your demo when you hit right you are rotating on the Z axis clockwise in a negative direction which to my understanding means that your camera is facing forward in the Z axis. You're going to have to use

 if(Input.GetKey("up"))
 {
    rigidbody2d.AddForce(transform.up * boostSpeed * 10);
 }
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 Slateboard · Jan 10, 2014 at 03:18 PM 0
Share

This worked. Thank you.

avatar image Wester243 · Jan 27 at 07:17 PM 0
Share

I have a problem with "boostSpeed" it says it doenst exist in the current context, but i dont know how to reference it

avatar image ziyadedris Wester243 · May 09 at 08:16 PM 0
Share

I think that that's because you didn't have a variable called boostSpeed in your script, and you don't need to, you just need to know that you should replace boostSpeed with the speed you want the object to travel at, and you should get rid of the 10 because that's a hardcoded value. And you should add a Time.deltaTime beside the boostSpeed variable because Time.deltaTime will make it so that the object will move at the same speed even if the game was laggy

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

25 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

Related Questions

App gets stuck on WebGL 0 Answers

Hello! Was wondering how do I make an object move randomly around the screen. And if it disappears at one side of the screen, it'll appear on the other side. 2 Answers

Check the position after drag and drop 0 Answers

Play sound once on collision 1 Answer

Laser beam using particle system Unity2d. 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