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 /
  • Help Room /
avatar image
0
Question by OttoClausen · Mar 27, 2016 at 10:28 AM · rigidbody2d-platformermoving platformchild-to-gameobjectinterpolate

Character on moving platform with Interpolation

I am making my first game in Unity and I really enjoy it, but I have run into a very annoying problem. My game is a platform game where the character is always running right. My character has a rigidbody2D component attached to it. I am now trying to implement platforms moving up and down and I am using this script:

 void Update()
 {
         transform.position = Vector2.MoveTowards (transform.position, patrolPoints [pointToReach].position,moveSpeed * Time.deltaTime);
 
         if (transform.position == patrolPoints [pointToReach].position) 
         {
             if (pointToReach == patrolPoints.Length - 1) 
             {
                 pointToReach = 0;
             }
             else
             {
                 pointToReach++;
             }
         }
     }

I move the character by changing the velocity of the Rigidbody2D component. That is done with the code:

 void FixedUpdate () 
     {
         if (alive && !levelWon) 
         {
             if (playerBody.velocity.x <= 0 && !onGround) 
             { 
                 playerBody.velocity = new Vector2 (playerBody.velocity.x, playerBody.velocity.y); 
             } 
             else
             {
                 playerBody.velocity = new Vector2 (moveSpeed, playerBody.velocity.y); 
             }
         }
     }

It works beautifully but my problem is when my character lands on the moving platform, he is not moving smoothly with the platform because he is not attached to it. I came across a solution that I really like that involves making the character a child of the platform as long as it is on it so I used that:

 void OnCollisionEnter2D(Collision2D target)
     {
         if (target.gameObject.tag == "MovingPlatform") 
         {
             transform.parent = target.gameObject.transform;
         }
     }
 
     void OnCollisionExit2D(Collision2D target)
     {
         if (target.gameObject.tag == "MovingPlatform") 
         {
             transform.parent = null;
         }    
     }

It works when I use the Interpolate setting None but the movement of my character is not smooth with this setting. I have been using interpolation with very smooth movement but it does not work with the moving platform. As soon as it lands on the platform and becomes a child of the platform it moves much more slowly than before, with extrapolation the problem is the opposite, he speeds up.

I hope I have made my problem clear, any thoughts or help is much appreciated

Thanks in advance - The confused game developer

Comment
Add comment · Show 14
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 Kutiman · Mar 27, 2016 at 01:48 PM 0
Share

Could you perhaps post your movement code? There could be many issues with this: 1. gravity - though should not be a problem. But maybe you can have some axis constraints that will solve it. 2. user going in and out its parent - due to its movement, it could potentially cause some jittering. 3. faulty movement code relative to parent

if you have more information, please provide.

avatar image OttoClausen Kutiman · Mar 27, 2016 at 04:07 PM 0
Share

I have updated the post so it now shows how I move the character too. Thank you for taking the time to help me out.

avatar image Kutiman OttoClausen · Mar 27, 2016 at 05:08 PM 0
Share

Well, Unity developers say not to directly change the velocity. Read it here: http://docs.unity3d.com/ScriptReference/Rigidbody-velocity.html

you have 2 choices as I see it:

  1. Use Rigidbody.AddForce(direction * speed * Time.deltaTime) : http://docs.unity3d.com/ScriptReference/Rigidbody.AddForce.html

  2. Or better for 2d, use translate on the transform, directly moving it a fixed distance with every frame transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime); : http://docs.unity3d.com/ScriptReference/Transform.Translate.html

Try to change it and see maybe it'll solve your issues.

Show more comments
avatar image Kutiman Kutiman · Mar 27, 2016 at 05:53 PM 0
Share

Share an exported project if you wish and we can take a look. Im at myUserName+at+gmail

avatar image TreyH · Mar 27, 2016 at 02:14 PM 0
Share

If you're moving with Update(), this will happen. Update() cycles are called as fast as your machine will allow every Update to finish. They are not in sync with physics updates that occur in sync with FixedUpdate(). As a result, you'll get jitter when colliders are near each other.

As for the platforms, have you looked into physics material and friction / drag?

avatar image OttoClausen TreyH · Mar 27, 2016 at 04:11 PM 0
Share

The platforms are moved up and down within Update() between two points. $$anonymous$$y character is controlled by setting the horizontal velocity to a variable moveSpeed within FixedUpdate(). I have posted the code for the characters movement

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by HarpDernier · Jul 03, 2018 at 06:10 PM

Hey I had this exact same problem. Hopefully this won't be my final solution but it seems to work in the mean time. In the script that parents the player to the moving platform I set the interpolation method to none when they connect and then set the interpolation method back to interpolate when they disconnect.

 public class StickToPlatform : MonoBehaviour 
 {
        Rigidbody2D BotBody;
 
     void Start ()
     {
         BotBody = GetComponent<Rigidbody2D> ();
     }
 
 void OnTriggerEnter2D (Collider2D info)
     {
         if (info.tag == "Moving Platform") 
         {
             transform.parent = info.transform;
             BotBody.interpolation = RigidbodyInterpolation2D.None;
         }
     }
     void OnTriggerExit2D (Collider2D info)
     {
         if (info.tag == "Moving Platform" && transform.parent == info.transform) 
         {
             transform.parent = null;
             BotBody.interpolation = RigidbodyInterpolation2D.Interpolate;
         }
     }
 }
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

60 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

Related Questions

2d Moving Platforms 1 Answer

Character flickering on moving platform 3 Answers

Rigidbody (player) moves slower on moving platform 2 Answers

Freeze character after collision 0 Answers

Moving RigidBody Smoothly 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