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
1
Question by PotatoInsertion · Jan 21, 2014 at 01:59 PM · physics2d-platformer2d-physics

2D moving platforms, physics issues

Hey all. I'm making a platform game with the Unity 2D tools. I have some moving platforms working, using Vector3.lerp. The problem here is when my character is on them. When the platforms move down, the character bounces up and down on it. When the platforms move up, the player does the same only less, it's more like he's vibrating.

From what I've looked into I'm thinking the issue is that the platforms move with

 transform.position = Vector3.Lerp(startMarker.position, endMarker.position, fracJourney);

while the character moves using 2D physics forces.

Can someone confirm if that's the issue? And if it is, how would I go about changing my current platform script (below) to use forces?

Thanks.

 void Start() {
         startTime = Time.time;
         journeyLength = Vector3.Distance(startMarker.position, endMarker.position);
     }
 
 
     void FixedUpdate() {
 
         if (atStartMarker){
             MovePlatformDown();
         }
 
         if (atEndMarker){        
             MovePlatformUp();
         }
     }
 
     void MovePlatformDown(){
 
         distCovered = (Time.time - startTime) * speed;
         fracJourney = distCovered / journeyLength;
 
         platform.transform.position = Vector3.Lerp(startMarker.position, endMarker.position, fracJourney);
 
         if (platform.transform.position == endMarker.position){
             //print ("Journey Down Complete");
             atStartMarker = false;
             atEndMarker = true;
 
             distCovered = 0.0f;
             fracJourney = 0.0f;
             startTime = Time.time;
         }
     }
 
     void MovePlatformUp(){
         
         distCovered = (Time.time - startTime) * speed;
         fracJourney = distCovered / journeyLength;
         
         platform.transform.position = Vector3.Lerp(endMarker.position, startMarker.position, fracJourney);
 
         if (platform.transform.position == startMarker.position){
             
             //print ("Journey Up Complete");
             atStartMarker = true;
             atEndMarker = false;
 
             distCovered = 0.0f;
             fracJourney = 0.0f;
             startTime = Time.time;
         }
     }
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 Calum-McManus · Jan 21, 2014 at 04:50 PM 0
Share

The issue is most likely to be the colliders. The platform is moving up or down, when moving down you start falling faster than the platform and the colliders hit causing the jumping, on the way up, you're forcing the platform collider to merge with the player collider and it starts shaking.

The best way i can think of right now is not to use normal physics, ins$$anonymous$$d have a ray cast pointing down checking for the platform using a tag, if the platform is there, then use GetComponent to find out the direction its travelling in and then move your player up or down at the same velocity. It's a bit long winded but it has worked for me xD

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Ferb · Jan 21, 2014 at 08:17 PM

I think you're probably right that the problem is caused by the platforms moving by having their positions changed in code, whereas the player is moved by physics forces. Admittedly, when I've had problems with that, it's been more often with things falling through each other rather than just shaking, but shaking wouldn't surprise me. These are the steps I think it'd take to change your moving platforms to use Unity's physics system.

  • Give each platform a rigidbody2D

  • Set them not to rotate, have gravitational scale of 0, have linear drag of 0

  • Give them a very high mass so the player won't move them by bumping into them

  • Set the rigidbody2D's velocity component manually

If you set the velocity to (endMarker.position - startMarker.position), that will start it going from the start point to the end point in 1 second, I think. Just multiply that vector by some float to change how long it takes. To test when to change the direction:

 fracJourney = Vector3.Dot(endMarker.position - startMarker.position,platform.transform.position - startMarker.position) / (JourneyLength * JourneyLength);

Should give you 0 when the platform is at the startMarker, 1 when it's at the endMarker, and something outside that range if it gets past either of those points (which it probably will, just fractionally, enough that you'll have to test for fracJournery>1 rather than fracJourney==1)

Comment
Add comment · Show 1 · 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 PotatoInsertion · Jan 22, 2014 at 10:59 AM 0
Share

Thanks for the reply, Ferb. I've done the above and it's definitely helped clean up the code (each platform function is now just one line), though I'm still having a similar visual problem; only now the character looks like he's vibrating whether it goes up for down. And when the platform reaches its highest point, the character hops up. I'm assu$$anonymous$$g this is all due to the forces being added to the character.

I've tried adding extra code to the player, something like Calum.$$anonymous$$c$$anonymous$$anus suggested above to check when he's on a platform, and then trying to equal out the forces, but having no luck. Any suggestions?

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

20 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

Related Questions

How would you make a pulling/sucking/attracting mechanic? 2 Answers

Using animated transform to push object away? 2D 0 Answers

Issues using physics for 2d platformer. 3 Answers

How to check if my enemy hits the ground at a certain velocity then add explosive force. 1 Answer

[2Dplatformer][Problem] When the player closes to the enemy, the enemy pass through the colliders 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