Hi, First post in a while… How goes it?
So let me jump right in to it… I’ve been trying to implement a platform that moves up and down. The moving platform will also move the player object which uses a Character Controller component.
Now for the most part it is working fine. The problem is that right at the point the platform changes direction (from upwards to downwards or vise versa) the player gets nudged either upwards or downwards… I think this is because the velocity is being calculated by the “current position” and “last position” and then being applied to CharacterController.Move(). Where as I pretty much need it all to happen at the same time because at the moment I think the player object is a frame behind.
The platform its self is being moved like so:
float step = speed * Time.deltaTime;
platform.position = Vector3.MoveTowards(platform.position, targetPoint.position, step);
When the target position is reached the target position changes.
The code that moves the player while on the platform is basically as follows:
platformCurrentPos = transform.position;
Vector3 velocity = (platformCurrentPos - platformPreviousPos) / Time.deltaTime;
playerController.externalMovement = velocity;
platformPreviousPos = transform.position;
The problem with this is that the platform has to move a small amount first before the velocity can be calculated and then sent to the CharacterController which results in a “delay” or “nudge” when the platform changes direction.
CharacterController.Move(OTHERSTUFF + externalMovement);
So I somehow need to get the new velocity of the platform at the time it happens rather than after it’s happened. Currently the following relies on the direction change happening first which isn’t working:
platformCurrentPos = transform.position;
XXXXX
XXXXX
platformPreviousPos = transform.position;
Is there by any chance a way to somehow “convert” the following:
float step = speed * Time.deltaTime; directly in to a “velocity” value i can send to “externalMovement” vector in CharacterController.Move(OTHERSTUFF + externalMovement)?
I really don’t want to make the player object a child of the platform as it will get in the way of other mechanics for example if the platform is rotating while the player is standing on it.
Orr if there’s another way…
Thanks!