character falls through mesh collider

Hello community,

I hope someone can point me in the right direction.

I have a simple platform with a mesh collider that goes up down.

The issues:

  1. if the platform speed is too slow, the character falls through platform when it reaches some random height

  2. if the platform speed is set higher, the character goes up on the way down, the char doesn’t stay on the platform so it falls onto the platform. Then on the way back up on the 2nd cycle, the char will fall through on some random height.

Here is a sample of the problem with a higher speed.
http://marklaurel.com/games/bugTrack/

This is the code for the platform:

#pragma strict
var pointB : Vector3;

var moveSpeed : float = 10.0; // higher number is slower

function Start ()
{
	var pointA = transform.position;
	while (true) {
		yield MoveObject(transform, pointA, pointB, moveSpeed);
		yield MoveObject(transform, pointB, pointA, moveSpeed);
	}
}

function MoveObject (thisTransform : Transform, startPos : Vector3, endPos : Vector3, time : float)
{
	var i = 0.0;
	var rate = 1.0/time;
	while (i < 1.0)
	{
		i += Time.deltaTime * rate;
		thisTransform.position = Vector3.Lerp(startPos, endPos, i);
		yield;
	}
}

Any help is greatly appreciated…

Thanks in advance.

-EDIT-

Ok, so I figured out I need to add some code to my char to allow it to move with the platform horizontally. I’m still not sure as to why it would fall through if the platform is moving vertically.

Anyone?

I suggest reading up on a tutorial about the collision system, notably the RigidBody and how to use it.

The short story is that when you change the transform position you’re actually teleporting the object. And when the character lands inside the platform the system let him fall through rather than doing anything else that would also be wrong.

What you need to do is use the RigidBody.MovePosition(Vector3 destination), that will “try” to move the character to the desired location and respect physics while doing it. So a wall will block it and it wont fall through floors.

thanks UnLogick, I’ll give that a go…

@Unlogick- I started looking into RigidBody(s). As I was looking around for information regarding proper implementation, I found this bit of info:

http://www.packtpub.com/article/unity-3.x-rigidbody-character-controller-script

From my understanding, the character controller may not be the best solution when external force is applied to it; that, RigidBody is a better solution.

The character is set up using character controller. I’m still new to all of this before I move forward; I’d like to know whether I need to change the character controller to a rigid body instead-- or is it possible to continue with the current set-up?

Thanks again…

The CharacterController is a specialized version of the RigidBody. It doesn’t support forces and it has a custom collision handling scheme. It is quite likely to suit your need.

But because there are several things that you simply can’t do with a CharacterController I usually recommend going all the way to the RigidBody and use the isKinematic = true setting. If you later need the object to be hurled by physics then its just a few simple lines of code to make the object react to applied forces properly. This also have the advantage of only needing to understand one physics component rather than two.

Thanks for the clarification, really helped!