Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 sihan416 · Jan 01, 2017 at 05:30 PM · movementrigidbody2dplatformer

Character dragging ground with it.

I have a character and a moving platform. The platform functions fine when the player is not moving. The platform defaults to moving left at a constant velocity. However when I move the character, it drags the platform along with it, so instead of the player moving, the platform and player are moving together with no player movement with respect to the platform. How would I get the player to not affect the platform velocity when moving? I have rigidbodies on both the player and the platform.

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 Grench1 · Jan 01, 2017 at 09:21 PM 0
Share

Are you using "RigdBody.addForce" or "transform.Translate". It is 2d sprites or you are using 3d object with ortographic perspective to get the 2d illusion. Post your code for both,player and moving plataform.

avatar image sihan416 Grench1 · Jan 01, 2017 at 11:05 PM 0
Share

I am using addForce on 2d sprites. So, I've changed my code a bit since then by trying to make only the ground moving and locking the x location of the character. But currently this causes the ground to stay in place ins$$anonymous$$d of moving also. Here are the relevant code:

 public class Road : $$anonymous$$onoBehaviour {
 
     public float speed = -0.5f;
 
     private Rigidbody2D rb2d;
 
     // Use this for initialization
     void Start () {
         rb2d = GetComponent<Rigidbody2D>();
     }
     
     // Update is called once per frame
     void Update () {
         
 
     }
 
     void FixedUpdate()
     {
         if (rb2d.velocity.x > speed)
             rb2d.AddForce(Vector2.left);
         else if (rb2d.velocity.x < speed)
             rb2d.AddForce(Vector2.right);
         float h = Input.GetAxis("Horizontal");
 
         rb2d.AddForce(Vector2.right * h * speed);
     }
 }

 public class Player$$anonymous$$anager : $$anonymous$$onoBehaviour {
 
 
     public float gravity = 1f;
     public float jumpPower = 4f;
     public bool grounded;
 
     private Rigidbody2D rb2d;
     private float speed = 0;
     //private Animator anim;
 
 
     private void Awake()
     {
         rb2d = GetComponent<Rigidbody2D>();
         rb2d.constraints = RigidbodyConstraints2D.FreezePositionX
             | RigidbodyConstraints2D.FreezeRotation;
     }
 
     // Use this for initialization
     void Start () {
 
     }
 
 
     // Update is called once per frame
     void Update () {
         //Here is the original call to move the player
         //rb2d.AddForce(Vector2.right *  3 * (baseSpeed - rb2d.velocity.y));
         
     }
 
     void FixedUpdate()
     {
 
         if(Input.GetButtonDown("Jump"))
         {
             speed = jumpPower;
         }
         
     }
 }

1 Reply

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

Answer by Grench1 · Jan 02, 2017 at 08:25 AM

I really recomend you to use Transform.Translate instead of Rigidbody.addForce. For the player:

 void FixedUpdate()
     {
         transform.Translate (new Vector3(Input.GetAxis("Horizontal") * Time.fixedDeltaTime * speed,0,0));
     }

and for the ground:

 void FixedUpdate()
     {
 
         transform.Translate (Vector3.left * speed * Time.fixedDeltaTime);
 
     }
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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

2d platformer player jitter when add velocity 1 Answer

Rigidbody2D jumping off of slopes 0 Answers

How to Move Platform up and down even if player stands on it. 2 Answers

Iam working on a platformer game and my player is getting stuck at random positions in the map by an invisible barrier. 1 Answer

I want to move the player once while he is 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