- Home /
Falling Through an Elevator
Hello,
Im having an issue with a really simple elevator. I got a simple movement script (with character controller) in my player, and another script for the elevator:
var pointB : Transform;
private var pointA : Vector3;
var speed = 1.0;
function Start () {
pointA = transform.position;
while (true) {
var i = Mathf.PingPong(Time.time * speed, 1);
transform.position = Vector3.Lerp(pointA, pointB.position, i);
yield;
}
}
My problem is that, when the elevator goes up with my player on it, the player just falls through, while when it goes down the player can stay on it. Both have a box collider attached.
I would like just to make the player go up with elevator. The rigidbodys didnt work, and a charactermotor neither.
Thanks!
Answer by DaveA · Mar 21, 2012 at 08:21 PM
Classic. The ground moves up into your character's collider, so it's now 'below' the floor, and in freefall. You either need to translate the character up too, enough so gravity won't kick him down, and/or turn off gravity for him while riding; or reparent the character to the elevator while he's riding, and unparent when he gets out.
Answer by aramusss · Mar 25, 2012 at 08:54 PM
Thanks for answering. I found that it could be solved with parenting both objects, but i have tryed many things and i cant solve it. Could you explain me how the script would be to parent both objects when they touch each other and then unparent when the other object exits? If you need more information, tell me. Thanks a lot!
Answer by dre38w · Mar 26, 2012 at 01:18 AM
I was having a very similar problem, but just now figured it out. On your elevator, do you have a box collider? I had to zero out my center parameters in the Inspector on my elevator's box collider for it to stop falling through.
Also I hear you need to have the character controller's (if you are using one) collision to be slightly above the characters feet. So basically their feet are sticking out of the bottom of the character controller oh so slightly. Me personally have never had that issue with the controller's positioning but maybe that'll help you.
You said you figured out the script for the parenting deal? If not here's one. =)
void OnTriggerStay(Collider col)
{
if (col.transform.tag == "Stick")
{
if (controller.isGrounded)
{
print("stuck");
transform.parent = col.transform;
}
}
}
void OnTriggerExit(Collider col)
{
if (col.transform.tag == "Stick")
{
print("not stuck");
transform.parent = null;
}
}
Hope it helps.