I’m going nut learning this collision stuff and cannot figure out where I am going wrong.
The image below shows a platform that in its fixedUpdate moves between the two waypoints (yellow dots) and my player has to hop on the moving platform to get to the other side.
I had this working fine whereby when the player landed on the top he stuck to the centre of the platform and they moved as one, but allowing for the player to turn to face left/right or jump at will.
BUT… if he missed the platform while trying to hop on and hit the side then it still acted as he had landed on the platform but stuck the played directly over the platforms pivot?
So now I test for the y value of the player and if it is above platform.transform.position.y + 0.7 then I know it has landed on top and so clamps to it, otherwise the player just bounces off.
BUT… not when he does land on top the players inertia slides him left and right as the platform hits a waypoint and changes direction?
Here is my code…what am I doing so wrong?
Thanks
void FixedUpdate() {
// move platform
GetComponent<Rigidbody2D> ().velocity = dir * speed;
// if platform has contact with player
if (hasContact) {
player.transform.position = new Vector2 (transform.position.x, player.transform.position.y);
}
}
void OnTriggerEnter2D(Collider2D coll) {
//direction change
dir = new Vector2 (-1 * dir.x, dir.y);
}
void OnCollisionEnter2D(Collision2D coll) {
// if I uncomment this line and comment the if statement out then the player sticks to platform
// but with the check for Y why does he slide?
//hasContact = true;
if (coll.gameObject.transform.position.y > transform.position.y + 0.7f) {
// as player has contact with top of platform set flag to true
hasContact = true;
}
}
void OnCollisionExit2D(Collision2D coll) {
// clear flag, player no longer in contact with platform
hasContact = false;
}
!(http:// picture hosting)