- Home /
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;
}
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
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.
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
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
![](https://koobas.hobune.stream/wayback/20220613094604im_/https://answers.unity.com/themes/thub/images/avi.jpg)