• 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 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 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 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 at 11:04 PM 0
Share

My 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 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 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 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.

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

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

Sphere spawn from border in direction (2D) 0 Answers

how do i check if the player stop moving/decrease speed/stop moving distance so i could stop the scroll animation background at the back. 2 Answers

Rigidbody2D is kind of flying instead of falling when the child box is colliding 1 Answer

Help with Missile 2D 1 Answer

Unity 2017: On disabling collider on object with body, entire GameObject is destroyed 0 Answers

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