Character Falling off Moving Platform

So I just started Unity a while ago but I am learning a lot at a very quick pace. Anyways, my character jumps and im trying to make a game with moving platforms. I have read a lot of forums and answers before making this post. But, the solution that I found online was to make a tag such as “movingPlatform” and then write a script so as when the character comes on the paltform he becomes it’s child and when he goes off he is no longer.

here is my script

//NEWCOD
function OnTriggerEnter(t:Collider)
{
if (t.gameObject.tag == “movingPlatform”)
{
Debug.Log("On trigger enter in " + gameObject.name);
gameObject.transform.parent = t.gameObject.transform.parent;
Debug.Log(“Parented”);
}
}

function OnTriggerExit(t:Collider)
{
if (t.gameObject.tag == “movingPlatform”)
{
Debug.Log("On trigger exit in " + gameObject.name);
gameObject.transform.parent = null;
Debug.Log(“Unparented”);
}
}
//ENDNEWCODS

when i set my platform is trigger my character goes through it as if its not there even if i add a rigid body to it … and after he falls through he becomes its child and the games keeps messing around what am i doing wrong.

Thank you…:slight_smile:

When a collider is a trigger, that means it will not collide with things, but will react when things pass through it. Use the normal collision events, and make the collider -not- a trigger.

Personally, I really dislike making a character a child of a platform; it detracts from the true essence of free motion. Instead, I make my character’s velocity as my character’s velocity + the platform’s velocity.

You might want to take a look at the 2D platform tutorial. Personally i have also encountered the same problem while making my own 2d platform a while back. And like u i used parenting which IMO doesn’t work and i have recently went through the 2D platform tutorial and i found out that, it uses OnCharacterControllerHit() and then assign the position of the platform when it hits and then calculate the position of your character.

After that, it uses simple maths by using the position of platform - player position (Or the other way round). Anyways then it use controller.Move to move according to the resultant position gotten from the above,

It seems a bit confusing but you can’t understand, gimme a pm and i hope that helps.

Links for the 2D platform tutorial. http://unity3d.com/support/resources/tutorials/2d-gameplay-tutorial.html

How might a script be written for this Tuah? sorry I am new I dont know how this can be doen exactly. Can you please explain further? Thank you

Zine92 can you please elaborate more on the idea of calculating player posiiton. Im makign a 3d game int he 2d tutorial they are still using the child method which doesnt seem to work for my game.

Use OnCharacterControllerHit to get a collision known as a CharacterControllerHit. This hit contains information about the collision, as shown here: Unity - Scripting API: ControllerColliderHit

Use that hit’s rigidbody’s velocity, and add it to the speed you want your character to move.

It’ll look something a little like this. (Sorry, I don’t use Javascript much.)

var platformVelocity = Vector3.zero;
function OnCharacterControllerHit( hit : CharacterControllerHit)
{
platformVelocity = hit.rigidbody.velocity;
}

I tried what you suggested and added a rigid body to the platform. Tagged it. Used a OnControllerColliderHit(hit:ControllerColliderHit)

instead of a trigger. But, it does not work the character just freezes on the platform and still doesnt move with it. I have no idea how to do this using velocity do you have the code or anything. Anyone else can help or adjust the code above for this to work please do.

Thank you.

Ikarah you’re on the right track. It took me a while to figure this out. Here is my solution.
Like you, I used the OnTriggerEnter to child the character to the platform when he jumped on. And made it so he is no longer a child when he jumps off.

1st problem:You can’t make the platform’s box collider a trigger because this will make the Character fall through the platform. Instead, make another GameObject with a Box Collider component and call it Trigger, set its isTrigger to true. Parent Trigger to your moving platform so it follows the platform’s movement. Make trigger cover the surface of the platform so that your character enters the trigger zone when he jumps on the platform.

2: Here is the Script I attached to my Player. It works I tested it, simply make sure the player has a Character Controller while the platform has a tag called “MovingPlatform” and the Trigger described in the previous point.

//START CODE
function Update () {
var controller : CharacterController = GetComponent(CharacterController);
if (!controller.isGrounded) {
Debug.Log(“Not grounded”);
transform.parent = null;
} else {
Debug.Log(“Is on the ground”);
}
}

function OnTriggerEnter(object: Collider) {
Debug.Log(object + " is inside OnTriggerEnter");
if(object.tag == “MovingPlatform”) {
Debug.Log(“The player is on the platform”);
transform.parent = object.transform;
}
}
//END CODE

OnTriggerExit would not work for some reason when the Player was parented to the platform. Luckily if we simply unparent the Player when they leave the ground we can achieve the desired result.

  1. ONE LAST THING! I don’t know if you will get this problem, but for some reason !controller.isGrounded would return true even when I was standing still. To fix this problem I made a slight modification to my ThirdPersonController Script:

In the ApplyGravity() function I replaced :

//CODE START
if (IsGrounded ()) {
verticalSpeed = 0.0;
Debug.Log(“We are gounded”);
}
else {
verticalSpeed -= gravity * Time.deltaTime;
Debug.Log(“Not grounded”);
}
//CODE END

with

//CODE START
if (IsGrounded ()) {
verticalSpeed = -0.1;
Debug.Log(“We are gounded”);
}
else {
verticalSpeed -= gravity * Time.deltaTime;
Debug.Log(“Not grounded”);
}
//CODE END

I made the verticalSpeed -0.1 instead of 0.0 when the character is on the ground.
This seemed to solve the problem and there is a whole thread concerned with the reason:http://forum.unity3d.com/threads/66133-Grounded.?p=575193&viewfull=1#post575193

I know the solution is a little lengthy, but it is not too hard to implement. I really figured it was time someone clearly solved this problem.

I had trouble with this too. I saw this and thought it was all unnecessary. The way I fixed it was instead of moving the platform like transform.position += new Vector3 (-0.1f * speed, 0, 0); or whatever I used rigidBody.velocity = new Vector3(-1f * speed,0,0);
So every frame the velocity would be the same moving in one direction. And it keeps any object on top of it as long as they have rigid bodies. The reason why is the other methods are just constantly changing the platforms position and not actually moving it. But when you change it’s velocity it actually moves. I hope this makes sense.

1 Like