- Home /
Parenting trigger moving platform
I want to have a player move with a platform whenever the player is on it. I have this script which works when the player jumps on the platform but not when he leaves.
My script is below. I don't know why is isn't working as it works with booleans but just not transforms!
var platform : Transform;
var player : Transform;
function OnTriggerEnter()
{
if(player.tag == "Player1")
{
player.transform.parent = platform.transform;
}
}
function OnTriggerExit()
{
if(player.tag == "Player1")
{
player.transform.parent = null;
}
}
I want to increase this to four players but it doesn't even work for one... Any help would be great.
Cheers, Peter
Is that script attached to the platform or the player ? Either you don't need to declare two transforms.
The script is attached to the trigger which is parented to the platform. Sorry, I don't fully understand your comment!
Answer by aldonaletto · Jan 12, 2012 at 11:01 AM
You should change this script a little, and attach it to the trigger object; the trigger object, in turn, must be childed to the platform.
function OnTriggerEnter(col: Collider) { if(col.tag == "Player") { col.transform.parent = transform.parent; } }
function OnTriggerExit(col: Collider) { if(col.tag == "Player") { col.transform.parent = null; } } When any character tagged "Player" enters the trigger volume, this script childs the character to the platform (the trigger's parent). When the character exits the trigger, the parenthood is broken. This will work for any number of players, provided that all of them have the same tag "Player".
Hey Aldonaletto. Thanks for replying. The OnTriggerEnter function works perfectly. However, the OnTriggerExit doesn't work and after leaving the volume the player continues to move! Do you know why this would happen?
Try to remove some rigidBody component in your moving platform if it has someone, it seems like a Unity bug that the OnTriggerExit does not work if you put a rigidBody component on it. I have this issue too and did it to solve.
i have an issue Aldo please help. $$anonymous$$y player is slimmed out/ Squashed/ thin. $$anonymous$$y player is changing scale but i have used this script to resize but does not work. Tried everything.
transform.localScale = Vector3(1,1,1);
This makes it very complicated and mess up my character
If you child the player to a non-uniform scaled object (different scales for x, y and/or z), it gets distorted. The solution is to make the platform an empty object with scale (1,1,1) and child the platform model and the trigger to it. The platform hierarchy should be something like this:
Platform <- empty object
$$anonymous$$odel <- cube scaled to the desired dimensions
Trigger <- box collider with the desired dimensions
The trigger script should be attached to the Trigger object, and refer to its parent, the Platform object (like in the answer above):
function OnTriggerEnter(col: Collider){
if (col.tag == "Player"){
// child the player to the trigger parent:
col.transform.parent = transform.parent;
}
}
function OnTriggerExit(col: Collider){
if (col.tag == "Player"){
// unchild the player from the platform
col.transform.parent = null;
}
}
Thank you sooo sooo much man #Respect you. Actually this is my first question that i understood with your answer. \really happy about it thank you :)