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
0
Question by EmpI · Jul 17, 2014 at 12:17 AM · playerobjectaddforcetowards

Rigidbody2D AddForce towards object

So I have a rigidbody2D component attached to my player. I have the player attach and detach to these rails I am trying to build. While on the rails I have the controls switch from the default WASD controls (which work as I want them to) to W and S for going up and down those rails to and from two nodes. Attaching to and from the rails work fine. However, the player only seems to want to move vertically no matter where I place the other nodes.

Put simply, I want the player to move towards and away from the node when pressing W and S respectively.

Here's the code for movement while on the rails (again, attaching to the rail and switching control configuration works as I want it to.)

                 //railroad movement
                 else if (Railroad == true) {
                     dir = Node2.transform.position - transform.position;
                     dir = dir.normalized;
                         if (Input.GetKey (KeyCode.W)) {
                 rigidbody2D.AddForce (dir * movement);
                     }
                         if (Input.GetKey (KeyCode.S)) {
                 rigidbody2D.AddForce (dir * -movement);
                     }
                 }

So what am I doing wrong?

Comment
Add comment · Show 1
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 rutter · Jul 17, 2014 at 12:19 AM 0
Share

That looks superficially okay, to me.

You might double check that dir is what you expect. Debug.DrawLine is handy for that, especially if you then call Debug.Break to pause at the end of the frame.

I'd also want to check that nothing else is keeping the rigidbody from moving freely. Gravity, constraints on position/rotation, collisions, and so on.

1 Reply

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

Answer by EmpI · Jul 17, 2014 at 01:24 AM

Okay I found the problem, i'm an idiot.

 if (Railroad == true) 
             {
                 rigidbody2D.velocity = new Vector2 (0, rigidbody2D.velocity.y);
             }

This code prevents the player from moving in the x axis at all when on the rails, it was something I added before realizing I might want my rails to move diagonally. Removing it fixed my problem completely. Thanks to Rutter for pointing out the problem might have been a programmatic constraint.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

NavMeshRenderer don't stop my player object when it collides with an static object 1 Answer

Look at Object 1 Answer

Player to Gui Object Communication in C# 1 Answer

Object Disappearing when comes close to player. 1 Answer

Rotating a player object without rotating the axis 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