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 Danao · Oct 09, 2014 at 02:49 AM · rigidbodyplayervelocityparentplatform

Getting the player to move with platform: Velocity, DistanceJoint, or...?

Hello, my player is parented to the platform OK if I move its position, but if I want a platform to move in a constant direction by applying velocity, the player just slides off. I've read what I could in other answers, but haven't found exactly what I need. Here's the code I'm trying to use:

 using UnityEngine;
 using System.Collections;
     
     public class MoveWithScript : MonoBehaviour {
         
     void  OnCollisionEnter2D (Collision2D other)
     { 
     if (other.gameObject.tag == "Player") {
         other.transform.parent = gameObject.transform;
         other.gameObject.rigidbody2D.velocity = gameObject.rigidbody2D.velocity;
         }
         }
                 
     void  OnCollisionExit2D (Collision2D other){ 
         if (other.gameObject.tag == "Player") {
         other.transform.parent = null;
         other.gameObject.rigidbody2D.velocity = other.gameObject.rigidbody2D.velocity;
         }
     }
 }

Thanks for reading. Your help is greatly appreciated :)

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by HarshadK · Oct 09, 2014 at 12:00 PM

Do not parent a rigidbody to another rigidbody. You will get quirky results.

Rather try joint to make your player stick to the platform.

Comment
Add comment · Show 5 · 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 Danao · Oct 09, 2014 at 12:36 PM 0
Share

Thanks for the advice Harshad$$anonymous$$. I've been seeing some of those quirky results as I've been trying different things! I'll look into using joint. If you know of any good examples or tutorials in an example like this, please point me in their direction :)

avatar image HarshadK · Oct 10, 2014 at 05:34 AM 0
Share
  • Distance Joint 2D.

  • DistanceJoint2D

You can create a joint at runtime wherein you add the DistanceJoint2D component to your platform and set player as the connectedBody. And to break the joint just apply the force greater than the break force of that joint.

avatar image Danao · Oct 11, 2014 at 01:49 AM 0
Share

Thanks Harshad$$anonymous$$. I'm still pretty new to all this, so I'm doing my homework on how to do what you stated above :D I tried to start with this, but it's also giving me pretty quirky results:

             void  OnCollisionStay2D (Collision2D collision){
 
         if (collision.gameObject.tag == "Player") {
             var disJoint = gameObject.AddComponent<DistanceJoint2D>();
             disJoint.connectedBody = collision.gameObject.rigidbody2D;
 }
     }
 }
     




avatar image HarshadK · Oct 13, 2014 at 05:26 AM 0
Share

Try to use the Anchor and Connected Anchor property of your joint so the two bodies are anchored properly.

Since this will allow you to join your two bodies specific to their current position with respect to each other.

avatar image Danao · Oct 13, 2014 at 11:11 AM 0
Share

O$$anonymous$$, thanks. I'll look into that next!

avatar image
0

Answer by Kiwasi · Oct 10, 2014 at 12:20 AM

You could make your code work as is just by changing to OnCollisionStay. Ultimately this will lock your players movement to match the platforms movement, which is typically undesirable.

A better method would be to compare the velocity of the player and the platform, and add force to the player proportional to the difference in velocities.

You could get even more realistic performance by getting your players mass right (A human typically weighs between 60-80 kg). With the right physics material this should provide enough force from gravity to hold the player to your platform at low accelerations. No scripts required.

Comment
Add comment · Show 6 · 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 Danao · Oct 10, 2014 at 12:24 AM 0
Share

Thanks Bored$$anonymous$$ormon. I'll look into using those options! I did trying comparing the velocities of the player and platform, then equating them. No luck there, though I'm missing why it doesn't work:

 void  OnCollisionEnter2D (Collision2D other)
     {
     if (other.gameObject.tag == "Player") {
     if (other.gameObject.rigidbody2D.velocity != this.gameObject.rigidbody2D.velocity) {
         other.gameObject.rigidbody2D.velocity = this.gameObject.rigidbody2D.velocity;




avatar image Kiwasi · Oct 10, 2014 at 03:21 AM 0
Share

You missed the first line of my answer.

OnCollisionEnter is only called once. So the players velocity matches the platform for one frame. Then physics happens and the player slides off.

OnCollisionStay will be called every frame the player and platform are colliding. Thus physics will be overridden.

You totally missed the point about comparing velocities. Try this

 void  OnCollisionStay2D (Collision2D collision){
     if (collision.gameObject.tag == "Player") {
         collision.rigidbody2D.AddForce(1000* (rigidbody2D.velocity - collision.rigidbody2D.velocity));
     }
 }


avatar image Danao · Oct 11, 2014 at 01:52 AM 0
Share

Sorry I misread your answer. Thanks for the reply. I was connecting the undesirable part to using OnCollisionStay2D. I tried the code you provided (and tried adjusting it), but something still isn't working. The player jitters and is forced left or right off the platform.

avatar image MrSoad · Oct 13, 2014 at 01:29 PM 0
Share

Does your platform need to be a rigidbody? If you can move the platform in the same way without having a rigidbody attached to it then parenting your player to it while in contact with it should work perfectly.

avatar image Danao · Oct 14, 2014 at 02:55 AM 0
Share

Thanks $$anonymous$$rSoad. I'm actually trying to use that method now and move the transform rather than move the rigidbody with velocity :)

Show more comments

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

29 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

Related Questions

Velocity powered rigidbody on a moving platform without parenting. 3 Answers

rigidbody.velocity not working on x axis 2 Answers

How to move your player with a moving platform (parent-child method not working). 2 Answers

Rigidbody player movement unrealistic velocity problem. 1 Answer

Velocity calculation for non-rigidbodies. 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