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 BlackWingsCorp · Mar 17, 2013 at 07:41 PM · javascriptplatformparenting

Player won't stay on moving floor

Hey guys what's up? I'm trying to make a 2d super mario-like game. It has movable floors that go either up/down or left/right in a loop. What I want to do is to set the player character's transform.position to be the same as the floor he's on as long as he's on one of them so that he moves with the floor but he keeps on falling. Here's the script:

 var player : GameObject;
 
 function OnCollisionEnter(col : Collision){
 if(col.gameObject.tag == "FloorUpDown"){
 transform.position.y = gameObject.transform.position.y;
 }
 else
 if(col.gameObject.tag == "FloorLeftRight"){
 transform.position.x = gameObject.transform.position.x;
 }
 }
 
 function OnCollisionExit(col : Collision){
 transform.position.x = player.transform.position.x;
 transform.position.y = player.transform.position.y;
 }
 
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
Best Answer Wiki

Answer by BlackWingsCorp · Mar 18, 2013 at 03:32 PM

Ok after hours i finally figured it out. Anyone with the same problem just place this script on the moving floors.

 var player : GameObject;
 var floor : GameObject;
 
 function OnCollisionEnter(col : Collision){
     if(col.gameObject.tag == "Player"){
     player.transform.parent = floor.transform;
     }
     }
 
 function OnCollisionExit(col : Collision){
 player.transform.parent = null;
 }
 

   
 

Hope it helps everyone with the same problem & I would like to than Auggie for giving me the idea of using the parent. 10x bro

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 Auggie · Mar 19, 2013 at 12:11 AM 1
Share

no problem. as long as it works... hehehe

avatar image raota8082 · Oct 16, 2020 at 08:07 PM 0
Share

I'm using this in 3D its work but there is one problem my player is shaking on the floor my floor is moving vertically up and down and I'm using a sphere as my player.

avatar image
0

Answer by Auggie · Mar 18, 2013 at 01:22 AM

i think something is wrong with your code. i can't tell if this is attached to the floor or to the player. if this is attached to the floor, try this:

 var player : GameObject;
  
 function OnCollisionEnter(col : Collision){
 if(this.gameObject.tag == "FloorUpDown" && col.gameObject.tag == "Player"){
 player.transform.position.y = this.gameObject.transform.position.y;
 }
 else
 if(this.gameObject.tag == "FloorUpDown" && col.gameObject.tag == "Player"){
 player.transform.position.x = this.gameObject.transform.position.x;
 }
 }
  
 function OnCollisionExit(col : Collision){
 player.transform.position.x = player.transform.position.x;
 player.transform.position.y = player.transform.position.y;
 }



or try doing it like:

 var player : GameObject;
  
 function OnCollisionEnter(col : Collision){
 if(col.gameObject.tag == "FloorUpDown")
 player.transform.parent = this.gameObject.transform;
 
 }
  
 function OnCollisionExit(col : Collision){
 player.transform.parent = null;
 }

i hope this helps

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 BlackWingsCorp · Mar 18, 2013 at 01:03 PM 0
Share

Sorry bro none worked. I'll try putting a bigger trigger - collider around the floors and replace the OnCollisionEnter/Exit by OnTriggerEnter/Exit. Anws 10x for trying to help. PS: the script is attached to the player

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

13 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

Related Questions

Parenting Objects 1 Answer

How to parent the state of a component with another one 0 Answers

Isometric camera skybox not working. 0 Answers

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Velocity powered rigidbody on a moving platform without parenting. 3 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