- Home /
Movement while moving along with other object.
Hi,
I am currently working on a game like crossy road. Have you seen how player moves across planks in river? I want to do exactly like that.
Methods I have tried so far:-
parenting game character to the planks for movement along the x axis.
moving game character along with plank.
Character movement method I am using :-
In update I am moving character towards an Vector3 variable EndPosition which is updated on every input.
void MoveForward() { //Next move position for player to move to newMovePosition = new Vector3 (EndPos.x, Player.transform.position.y, EndPos.z+1); //Check if there is no obstacle in front RaycastCheck (Vector3.forward); //Update next move position only if there in no obstacle if(AbleToMove) { ContinueMove (newMovePosition); ScoreControl.ScoreUpdate(); } }
Similar for Left,Right and Backwards movement.
If I use temporary variable to update Next or End movement position, while player is on plank which is moving along X axis, so when player moves it updates its position from the spot it landed which is completely wrong.
If I update End position form player current transform.position it works but movement is not smooth while player moves left or right while its on plank or when he moves in and out of it. Sometimes character object moves double the amount of what it's supposed to move While moving left or right.
When I am moving character along with the plank, player gets stuck on its edge. as it dosen't completes it original movement and executes code to move along with it.
After every movement I am snapping its transform position so it stays in grid.
//Check the from end position if ( Mathf.Abs(EndPos.z - Player.transform.position.z) < snapToGridDistance && Mathf.Abs(EndPos.x - Player.transform.position.x)<snapToGridDistance) { //Snap player position so it stays in grid. Player.transform.position = new Vector3(Mathf.Round(Player.transform.position, Player.transform.position.y, Mathf.Round(Player.transform.position.z));
I have been stuck with this problem from past month. Please help.
Your answer
