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 darkhog · Dec 19, 2015 at 06:48 PM · physicsrigidbodymoving-platformfun

Alternative methods of making character move with 3d moving platform?

I'm asking, because my usual solution (dynamic parenting to platforms) causes me lot of issues (scale and object not always staying on a platform) and solutions that require things like physics materials are out of a question as well, because when I tried them, player was moving like through molasses when on a moving platform.

Are there any other ways of making player move with a 3d moving platform? Player is a rigidbody and so is the platform.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by LazyElephant · Dec 27, 2015 at 08:38 AM

A simple solution would be to set the player velocity to match the moving platform velocity when the player lands.

 void OnCollisionEnter(Collision collision){
      if( collision.gameObject.tag == "Platform" ){
            gameObject.GetComponent<Rigidbody>().velocity = collision.gameObject.GetComponent<Rigidbody>().velocity;
      }

Edit: To get this working in my test, I had a cube acting as the platform and a capsule as the player, both with rigidbodies attached. The only settings I changed on the rigidbodies were to set the platform rigidbody to kinematic and to constrain the x and z rotation of the capsule so it wouldn't roll over.

On the capsule, I added these two scripts:

 using UnityEngine;
 using System.Collections;
 
 public class MovePlayer : MonoBehaviour {
     Rigidbody rbody;
     
     void Start () {
         rbody = gameObject.GetComponent<Rigidbody>();
     }
     
     void FixedUpdate () {
         float xAxis = Input.GetAxis("Horizontal");
         rbody.AddForce(Vector3.right * 20 * xAxis);
     }
 }

and

 using UnityEngine;
 using System.Collections;
 
 public class PlayerCollision : MonoBehaviour
 {
     void OnCollisionEnter(Collision collision)
     {
         print("CollisionEnter");
         gameObject.GetComponent<Rigidbody>().velocity = collision.gameObject.GetComponent<Rigidbody>().velocity;
     }
 }

On the platform, I used this one script:

 using UnityEngine;
 using System.Collections;
 
 public class MovePlatform : MonoBehaviour {
     Rigidbody rbody;
     public Vector3 targetPos = new Vector3(5, 0, 0);
     float elapsedTime = 0f;
     
     // Use this for initialization
     void Awake() {
         rbody = gameObject.GetComponent<Rigidbody>();
     }
     
     // Update is called once per frame
     void FixedUpdate () {
         elapsedTime += Time.deltaTime;
         rbody.MovePosition(targetPos * elapsedTime );
     }
 }
 

To simulate dropping onto the platform, I started the capsule a short distance above the platform. The result was a capsule that didn't slide at all when it landed on the platform and was still able to move with my input.

Comment
Add comment · Show 3 · 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 darkhog · Dec 27, 2015 at 09:29 AM 0
Share

Thought about it, but it would have that unfortunate con that it would make impossible for player to move around the platform, since it would be have to be applied in OnCollisionStay as player is a rigidbody. Also put it as an answer.

avatar image LazyElephant darkhog · Dec 27, 2015 at 09:37 AM 0
Share

If you're moving the platform with the physics system, using it once in OnCollisionEnter should work. When I tested the solution, I had a kinematic rigidbody on the platform and moved it with rigidbody.$$anonymous$$ovePosition. I used a capsule with a non-kinematic rigidbody attached to represent the player. When the capsule landed on the platform, it didn't slide at all. I didn't test moving it after, but since the velocity was only set in the OnCollisionEnter function, it shouldn't effect your ability to move.

avatar image LazyElephant darkhog · Dec 27, 2015 at 10:04 AM 0
Share

I edited my answer to give the code I used. Unless there is something specific to your implementation that prevents it, this solution should work.

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

40 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

Related Questions

Moving a rigidbody in local space with transform.TransformPoint has unexpected results. 1 Answer

Adding Gravity to a game object to make a black hole sucking effect. 1 Answer

Move and rotate rigid body using 6DOF marker location from file 0 Answers

[3d]Rigidbodies are not working as expected with animations, They are not stopping on colliding, Am I doing anything wrong? 0 Answers

JointDriveMode is obsolete, any alternatives? 2 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