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 sirhotalot · Jun 21, 2015 at 11:33 AM · rigidbody2dvelocityparentchildplatform

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

Edit: Fixed it. I tried the parent-child method again and noticed when I wasn't on the platform the player moved very slightly. I decided to completely scrap my moving platform and start over using this tutorial and it's working now.

I've searched but couldn't find an answer to my particular problem. I've tried setting the player as a child of the platform but that didn't work. I've since removed that code in favor of trying to move the player directly using velocity and AddForce, but that's not working either. Here's my code for the moving paltform:

using UnityEngine; using System.Collections;

public class MovingBlock : MonoBehaviour {

 public bool canMove = false;
 public bool playerOn = false;
 public float vSpeed = 1f;
 public int vDirection = 1; //1 equals UP -1 equals DOWN
 public float hSpeed = 1f;
 public int hDirection = 1; //1 equals UP -1 equals DOWN

 private GameObject player;

 // Use this for initialization
 void Start () {
     player = GameObject.FindWithTag ("Player");
 }
 
 // Update is called once per frame
 void FixedUpdate () {
     if (canMove == true) {

         rigidbody2D.velocity = new Vector2 ((hSpeed * hDirection), (vSpeed * vDirection));

     }
     if (playerOn == true) {

         player.rigidbody2D.AddForce(new Vector2(hSpeed, vSpeed));
         //Debug.Log("Player: " + player.rigidbody2D.velocity);

     }
 }

 void OnTriggerEnter2D(Collider2D other) {

     if (other.gameObject.name == "MovingPlatformCD") {
         vDirection = vDirection * -1;
         hDirection = hDirection * -1;
         //Debug.Log ("HIT!");
     }

 }

 void OnCollisionEnter2D(Collision2D other){

     if (other.gameObject.CompareTag("Player")) {
         //Debug.Log("Player has landed on platform!");
         //MakeChild();
         playerOn = true;
     }

 }

 void OnCollisionExit2D(Collision2D other){

     if (other.gameObject.CompareTag("Player")) {
         //ReleaseChild(); 
         playerOn = false;
     }

 }

 void MakeChild(){
     player.transform.parent = this.transform;
     playerOn = true;
 }
 
 void ReleaseChild(){
     player.transform.parent = null;
     playerOn = false;
 }

}

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 Socapex · Jun 21, 2015 at 12:03 PM

Pseudo code (use bounds to find height):

  if (playerOn == true) {
 
      player.transform.position = transform.position + playerHeight / 2 + paltformHeight / 2;
      //Debug.Log("Player: " + player.rigidbody2D.velocity);
 
  }
Comment
Add comment · Show 2 · 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 sirhotalot · Jun 21, 2015 at 09:08 PM 0
Share

Here's what I have now. It throws the player across the world:

void FixedUpdate () {

     if (can$$anonymous$$ove == true) {

         rigidbody2D.velocity = new Vector2 ((hSpeed * hDirection), (vSpeed * vDirection));

     }
     if (playerOn == true) {

         Vector3 playerHeight = player.GetComponent<BoxCollider2D> ().bounds.max;
         Vector3 platformHeight = player.GetComponent<BoxCollider2D> ().bounds.max;

         Debug.Log("Player height: " + playerHeight);
         Debug.Log("Platform height: " + platformHeight);
         player.transform.localPosition = transform.localPosition + ((playerHeight / 2) + (platformHeight / 2));

     }
 }


Edit: Fixed it. I tried the parent-child method again and noticed when I wasn't on the platform the player moved very slightly. I decided to completely scrap my moving platform and start over using this tutorial and it's working now.

avatar image Socapex · Jun 23, 2015 at 01:00 AM 0
Share

I must have been tired, because if you use physics then my solution is bad. You could manage it, but you shouldn't affect the transform.position of a physics enabled object. Anyways, glad to hear it works :)

avatar image
0

Answer by lugduweb · Jun 22, 2015 at 03:50 AM

My question is maybe stupid, but did you define a collision volume that moves with the platform ? If so parenting player to the platform OnCollisionEnter shall work (this is how I did).

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Make a simple tree 1 Answer

Decrease rigidbody2D velocity per object attached to parent 1 Answer

Player parent to platform problem 0 Answers

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

Making Rigidbodies the parent of character controllers is giving me problems. 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