Jumping/Moving while on a moving platform (crossy road style)
I'm trying to make a simple Crossy Road clone kind of thing.
All is working good; except when the player jumps on a moving platform. I use the following code on the platform:
void OnTriggerStay(Collider coll) {
if (coll.tag == "Player") {
// Player is on the platform, let's parent it
coll.transform.parent = transform;
// We're hooked up
coll.GetComponent<PlayerController>().isHooked = true;
}
}
void OnTriggerExit (Collider coll) {
coll.transform.parent = null;
coll.GetComponent< PlayerController >().isHooked = false;
}
It works well, when the player is on the platform then it moves with it.
But all the sudden, all my controls on the player stop working and it behaves very erratically. Like if the move factor is 2 when player is not on the platform (like transform.position.x + 2) on the platform it suddenly becomes like baby steps, around 0.2.
'transform.position' when the game object is always in world position right ? no matter if the player is child or not.
What obvious thing i'm missing here ?
UDATE: code
void FixedUpdate() {
currentLerpTime += Time.deltaTime * lerpSpeed;
perc = currentLerpTime;
if (!isHooked) {
transform.position = Vector3.Lerp(startPosition , endPosition, _animationCurve.Evaluate(perc));
}
}
You might be setting/using the local position for the child object. You might also be affected by a scale change (ie. moving by some factor of the localscale, which changes depending on your parent object).
What does isHooked to in the playercontroller? I'd guess that's the culprit. It might also be that there's situations where your player is quickly getting parented/deparented if it's in contact with two platforms.
Thanks, for the scale that was it ! Awesome !
But i still can't get out of the platform once i'm on it. I updated the question with the code in PlayerController
So you're only moving once you're not "hooked". And you only stop being hooked if you move off the platform. That might be the issue.