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 Kilgore999 · Jan 04, 2021 at 10:26 PM · 2drigidbody2dmovement script

My spaceship does not move forward when mouse is pressed down

To summarize what I want it to do: I am trying to code it so that when one button is pressed, the player's ship will start moving forward in the direction that it is facing. If a different button is pressed, the player's ship will start moving backward from the direction that it is facing.
I starting coding it with rigidbody2d, but it doesn't move forward or backwards when I press them down. Tried many times to fix it, to figure out where I am going wrong, but I cannot find out what code is being problematic. Here is the code right now:

 public class SpaceShip : MonoBehaviour
 {
     public float acceleration;
     private Rigidbody2D rb2d;
 
     private Vector2 forward;
 
     void Start()
     {
         rb2d = GetComponent<Rigidbody2D>();
     }
 
     private void FixedUpdate()
     {
         forward = transform.position + transform.forward;
         if (Input.GetMouseButton(0))
         {
             rb2d.AddForce(forward * acceleration);
         }
         else if (Input.GetMouseButton(1))
         {
             rb2d.AddForce(-forward * acceleration);
         }
     }
Comment
Add comment · 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 Llama_w_2Ls · Jan 04, 2021 at 10:36 PM 0
Share

You dont need to add transform.position in forward = transform.position + transform.forward;. Just have it set to transform.forward as the direction. This should work. Is your rigidbody kinematic or maybe static?

avatar image yummy81 Llama_w_2Ls · Jan 04, 2021 at 10:44 PM 1
Share

That's right. AddForce takes direction as its argument. Not point. But changing it to transform.forward won't work either since this is 2D game and OP declared "forward" as Vector2. It should be for example transform.up.

avatar image Kilgore999 yummy81 · Jan 04, 2021 at 11:04 PM 0
Share

$$anonymous$$y rigidbody is dynamic, not kinematic nor static. I tried just setting the direction to transform.forward and even transform.up. Both times, the ship doesn't move. The thing is that I somehow got it to work using transform.forward but now it doesn't move at all.

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by CmdrZin · Jan 05, 2021 at 04:04 AM

Ran your script. Set acceleration to 100 in Editor.
Added a Debug.Log() that showed your forward calc was always 0.0. You also didn't specify the ForceMode2D mode. Don't know what the default is if you don't have it.

             Debug.Log("LMB " + forward);
 //            rb2d.AddForce(forward * acceleration, ForceMode2D.Force);
             rb2d.AddForce(Vector3.up, ForceMode2D.Force);

seemed to work for moving upwards.

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
0

Answer by arbellrm · Jan 05, 2021 at 08:22 AM

Do you set the acceleration value anywhere? If that's not set (or 0 for that matter), the AddForce function won't have anything to add.

When you debug, does it reach the AddForce line? If so, what value does the (forward * acceleration) have?

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
0

Answer by Delgoodie · Jan 05, 2021 at 01:32 PM

I think CmdrZin solved your problem but I can't help but notice you are replacing the transform.forward vector with a vector that is dependent on position. This means it will move differently based on where it is in the map and since that vector is not normalized it will move faster as it gets farther from 0, 0. I think you just want to use transform.forward instead of forward.

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

270 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 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 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

How to freeze all enemy objects at once 1 Answer

Unity 2d mobile movement 0 Answers

Player Bump on map border 0 Answers

Using direction and Rigidbody2D.AddForce() to move towards object. 1 Answer

How to stop spam jumping? 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