Character shaking on moving platform (elevator)
Hey guys. Could someone help me? Whats the best way to move something like an elevator? I tried many ways but when my Character controller is standing on a rising platform he starts to shake (i'm guessing the elevator collider hits characters collider). Some say to parent the player from the elevetor onTriggerEnter but in my case it does nothing. I use standard Character controller for the player with applying vertical velocity for gravity in .Move() method I also use a SpereCast for ground checking, the elevator model only has a generated collider the one you get when importing an fbx model and right now I use Vector3.MoveTowards to move it up and down. Any help would be great thanks in advance.
Also, after some experimentation I found that if my vertical velocity (gravity) is equal to 0 when I'm grounded, character just falls through the elevator, but if Y velocity isn't 0 (I have it at 6f) he doesn't fall but the stuttering occurs...
Answer by ancientpixel · Dec 04, 2020 at 01:05 AM
I have the exact same problem, a shame there are no answers yet. Actually that's true for most threads where it seems that that must have been solved before.
I ended up trying to get the difference between my elevator position and last frame position and add that to whatever enters the trigger- but it always returns 0. That may be an hierarchical problem. Currently I don't get why it would not calculate the difference right, but I'll look at it again tomorrow.
[TOMORROW EDIT]
ok, ive got half of it - I used an empty with Character Controller and had a Child with a capusle Collider and Rigidbody so i could bump into physic objects like boxes and stuff, that was part of the problem. Now I moved the Collider and Rigidbody to the empty where the Character Controller is and in the Elevator Sticky Zone I used the difference position as a kind of velocity for everything that is on the elevator:
void Update(){
Vector3 dif = transform.position - previousPos;
foreach (GameObject o in objs) {
if(o.GetComponent<CharacterController>() != null) {
o.GetComponent<CharacterController>().Move(dif);
} else {
o.transform.position += dif;
}
}
previousPos = new Vector3(transform.position.x, transform.position.y, transform.position.z);
}
objs is a list of gameObjects that gets filled and emptied on TriggerEnter and Exit @gbagiryan
This works for left, right and up movement, the down movement is somewhat jerky again, I suspect bc of gravity or the antificial gravity in the character controller - with 0 gravity it works fine. 0 gravity messes up how you walk over slopes though, so you need something that zeroes it on the elevator.
Answer by BenjaminWolfgang · Mar 06, 2021 at 12:44 PM
private void OnTriggerEnter2D(Collider2D other) { if(other.gameObject.tag == "Player") { other.gameObject.transform.parent = this.gameObject.transform; hasEntered = true; } }
private void OnTriggerExit2D(Collider2D other)
{
if (other.gameObject.tag == "Player")
{
other.gameObject.transform.parent = null;
hasEntered = false;
}
}
Your answer
Follow this Question
Related Questions
Problem with the Character Controller on the Y-Axis,Character Controller drifting in the Y Axis 0 Answers
How to move my rigid body in the direction my model is facing. 0 Answers
Problem with Jump script 1 Answer
i'm using a code to make my cube walk with character controller but when i play the cube spins. 0 Answers