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 /
avatar image
0
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 shift platform. I've seen many people make a simple script to add the player to the platform as a child, making it move with the platform. However, as I've played around with this I've found that when my player (a simple cube) get parented, it's position and more so it's scale go crazy.

This 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 (this.transform.position.x >= 6 || this.transform.position.x > -6 && direction) 
     {
         this.transform.Translate (Vector3.left * (platformSpeed * Time.deltaTime));

         if (this.transform.position.x <= -6) 
         {
             direction = false;
         }
     }

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

         if (this.transform.position.x >= 6) 
         {
             direction = true;
         }
     }
 }

And this is a just for the player to stick to platforms, movement is another script.

 void OnCollisionStay (Collision platform)
 {
     this.transform.parent = platform.transform;
 }

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

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

alt text

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

glitchy.png (9.4 kB)
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 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.Get$$anonymous$$eyDown ($$anonymous$$eyCode.Space)) 
         {
             this.GetComponent<Rigidbody> ().AddForce (Vector3.up * jumpHeight);
             grounded = false;
         }
     }

     if (!grounded)
     {
         //Forward Rotation
         if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.LeftArrow)) 
         {
             this.transform.Rotate (new Vector3 (0, 0, rotationSpeed * Time.deltaTime));
         }
     

         //Backwards Rotation
         if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.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
0
Best Answer

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

Good day!!

One tip:

When you say "this" in the script, you are refearing to this 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 which is a class)

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

Then ,talking about childs and parents. You must care of relations of parameters like rotations, scales, positions... when child-unchild an object. If the parent and the child 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 childs, in the same "level of parenthood". This 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
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 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
0

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

I just want to show how I fixed this issue for anyone else who had this issue. So like @tormentoarmagedoom said, you want a third 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.GetChild(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 child you will get an error. So that was just a little fix for that.

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

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

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

Spawning animals at a random postion on platforms. 2 Answers

2D 360 degress platformer example needed 0 Answers

Getting an object to re-spawn/ transform player back to the beginning 1 Answer

Player moving with platform 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