• 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
Question by TheSandwichMan · Sep 27, 2017 at 09:39 AM · platformerplatformplatforms

Making a Child/Parent Based Moving Platform?

So I'm learning C# and am messing around with a s$$anonymous$$ft platform. I've seen many people make a simple script to add the player to the platform as a c$$anonymous$$ld, making it move with the platform. However, as I've played around with t$$anonymous$$s I've found that when my player (a simple cube) get parented, it's position and more so it's scale go crazy.

T$$anonymous$$s is my script for the platform.

 public float platformSpeed = 2.5f;
 public bool direction = true; //true = left, false = Right
 public GameObject player = null;
 private Vector3 scale;

 void FixedUpdate ()
 {
     //Left Direction
     if (t$$anonymous$$s.transform.position.x >= 6 || t$$anonymous$$s.transform.position.x > -6 && direction) 
     {
         t$$anonymous$$s.transform.Translate (Vector3.left * (platformSpeed * Time.deltaTime));

         if (t$$anonymous$$s.transform.position.x <= -6) 
         {
             direction = false;
         }
     }

     //Right Direction
     else if (t$$anonymous$$s.transform.position.x <= -6 || t$$anonymous$$s.transform.position.x < 6 && !direction) 
     {
         t$$anonymous$$s.transform.Translate (Vector3.right * (platformSpeed * Time.deltaTime));

         if (t$$anonymous$$s.transform.position.x >= 6) 
         {
             direction = true;
         }
     }
 }

And t$$anonymous$$s is a just for the player to stick to platforms, movement is another script.

 void OnCollisionStay (Collision platform)
 {
     t$$anonymous$$s.transform.parent = platform.transform;
 }

 void OnCollisionExit ()
 {
     t$$anonymous$$s.transform.parent = null;
 }

Also here's just an image of what it looks like.

alt text

If anyone knows how to fix t$$anonymous$$s problem it would be much appreciated. I'm completely stumped at t$$anonymous$$s point.

glitchy.png (9.4 kB)
Comment

People who like this

0 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 TheSandwichMan · Sep 28, 2017 at 09:23 AM 0
Share

So I'm still having trouble with this, and I thought I'd add some more information.

Like here is the script I'm using for movement. The player can only jump straight up using Unity rigidbody restrictions.

 public float rotationSpeed = 500.0f;
 public float jumpHeight = 14500.0f;
 public float gravityForce = 500.0f;
 public float startHeight = 0.8f;
 public bool grounded = true;

 //Jump Landing
 void OnCollisionEnter ()
 {
     grounded = true;
 }

 void Update ()
 {
     //Gravity
     this.GetComponent<Rigidbody> ().AddForce (Vector3.down * gravityForce);

     //Jump
     if (grounded) 
     {
         if (Input.GetKeyDown (KeyCode.Space)) 
         {
             this.GetComponent<Rigidbody> ().AddForce (Vector3.up * jumpHeight);
             grounded = false;
         }
     }

     if (!grounded)
     {
         //Forward Rotation
         if (Input.GetKey (KeyCode.LeftArrow)) 
         {
             this.transform.Rotate (new Vector3 (0, 0, rotationSpeed * Time.deltaTime));
         }
     

         //Backwards Rotation
         if (Input.GetKey (KeyCode.RightArrow)) 
         {
             this.transform.Rotate (new Vector3 (0, 0, -rotationSpeed * Time.deltaTime));
         }
     }            
 }

The other thing I thought worth mentioning is that the player's transform goes crazy, only when the rotation is offset. If it is (0,0,0) it seems to work fine.

I feel like this is a simple fix I'm just missing something. I'm slowly going insane so a fix would be much appreciated.

2 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by tormentoarmagedoom · Sep 28, 2017 at 09:03 AM

Good day!!

One tip:

When you say "t$$anonymous$$s" in the script, you are refearing to t$$anonymous$$s script atached to the gameobject, not the gameobject. For some functions, it works as gameobject, but if you are used to do it, you will have errors for sure.

If want to refear the object where the script is placed, use "gameObject" (not GameObject w$$anonymous$$ch is a class)

 void OnCollisionStay (Collision platform)
  {
      gameObject.transform.parent = platform.transform;
  }
  void OnCollisionExit ()
  {
      gameObject.transform.parent = null;
  }
 

Then ,talking about c$$anonymous$$lds and parents. You must care of relations of parameters like rotations, scales, positions... when c$$anonymous$$ld-unc$$anonymous$$ld an object. If the parent and the c$$anonymous$$ld does not ahve the same scale/rotation, it can give you problems.

As a good practice, if you want to parent/unparent objects during runtime, i recomend you to create an empty GameObject that have all the real objects to be linked as c$$anonymous$$lds, in the same "level of parenthood". T$$anonymous$$s way, the parent for all will have always scale (1,1,1), rotation (0,0,0), etc... so will not be affected if parent/unparent from it.

Please Accept the answer as correct and/or ask more using @tormentoarmagedoom !

Bye! :D

Comment

People who like this

0 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 TheSandwichMan · Sep 28, 2017 at 09:22 AM 0
Share

Thanks for trying to help me out. Though I tried calling the gameObject, and assigning the player as a gameObject, and calling that, but neither seemed to change it. When it lands on the platform while rotated, it still freaks out.

Whenever you can give my code a better look over would be a big help. :)

avatar image

Answer by TheSandwichMan · Sep 29, 2017 at 02:07 AM

I just want to show how I fixed t$$anonymous$$s issue for anyone else who had t$$anonymous$$s issue. So like @tormentoarmagedoom said, you want a t$$anonymous$$rd party object to parent the player and the platform too. Though, if you just make an empty game object at (0,0,0) it won't make the play stick to the platform, though that might just be the way I'm moving my platform.

So instead I parented the platform, to an empty game object, and then called the empty game object up in the script to then parent to the player.

 void OnCollisionStay (Collision platformCollision)
 {                
     GameObject platformCenter = platformCollision.transform.GetC$$anonymous$$ld(0).gameObject;

     gameObject.transform.parent = platformCenter.transform;    
 }
     
 void OnCollisionExit ()
 {
     gameObject.transform.parent = null;
 }

I had also added an if statement to check for a tag "Platform", because if an object doesn't have a c$$anonymous$$ld you will get an error. So that was just a little fix for that.

Anyways I hope t$$anonymous$$s helps anyone in the future who has t$$anonymous$$s same issue, it's maddening I know.

Comment

People who like this

0 Show 0 · 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

71 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Platform pathfinding 0 Answers

Can you embed an AR Foundation project into a web browser? 0 Answers

Player not sticking to moving platform 1 Answer

2D Platformer Moving Platform Question 3 Answers

Player slides off from moving platform when player isn't moving Bolt/visual scripting 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