- Home /
[Character Controller] Jump-Movement in other direction while beeing in midair
Hey there,
so i managed to move my character with a character collider (via vectors and .SimpleMove) Everything works quite well, except the Jump. I am working on an iOS 2D Plattformer game, so i have four Buttons (Move Left, Move Right, Attack and Jump) The Jump-Button does it's job, but there is a little problem.
These are the four scenarios:
when i move to the left (pressing the MoveLeft-Button, the Character flips to the left) AND the Jump-Button it's jumping to the left and playing the Jump-Animation. Everything allright.
when i move to the right (pressing the MoveRight-Button, the Character flips to the right) AND the Jump-Button it's jumping to the right and playing the Jump-Animation. Everything allright.
when i jump (pressing the Jump-Button, the Character plays the Jump-Animation) AND the MoveLeft-Button it's moving in midair to the left and playing the Jump-Animation to the end. Everything allright.
when i jump (pressing the Jump-Button, the Character plays the Jump-Animation) AND the MoveRight-Button it's moving in midair to the right and playing the Jump-Animation to the end. Everything allright.
BUT:
when i press the MoveLeft-Button and then the Jump Button to get in midair AND then pressing the MoveRIGHT-Button the Character flips to the right but moves in the left direction. Same behaviour vice versa.
Here is an excerpt of my code:
void moving ()
{
isButtonDown = false;
foreach (Touch touch in Input.touches) {
if (guiTexture.HitTest (touch.position)) {
isButtonDown = true;
if (touch.phase != TouchPhase.Ended && touch.phase != TouchPhase.Canceled) {
if (!lookRight) {
player.GetComponent<exSprite> ().HFlip ();
lookRight = true;
script2.lookLeft = false;
}
if (script.inAir == true) {
direction = transform.TransformDirection (Vector3.right);
controller.SimpleMove (direction * speed);
Debug.Log ("IN DER LUFT RECHTS!");
} else {
script.inAir = false;
lookRight = true;
direction = transform.TransformDirection (Vector3.right);
controller.SimpleMove (direction * speed);
if (!player.GetComponent<exSpriteAnimation> ().IsPlaying ("RitterMove")) {
player.GetComponent<exSpriteAnimation> ().Play ("RitterMove");
playingAnim = true;
}
}
} else if (touch.phase == TouchPhase.Ended) {
playingAnim = false;
if (playingAnim == false) {
player.GetComponent<exSpriteAnimation> ().IsPlaying ("RitterMove");
player.GetComponent<exSpriteAnimation> ().Stop ();
}
}
}
}
}
That was the moving()-Method in the MoveRight-Script which is called in the Update()-Method, the MoveLeft-Script is pretty much the same, except the Vector3 goes to the left, not to the right.
Here is the jump-method:
void LateUpdate ()
{
direction.y -= gravity * Time.fixedDeltaTime;
// Move the controller
controller.Move(direction * Time.fixedDeltaTime);
if(controller.isGrounded)
{
inAir = false;
canJump = true;
Debug.Log("BIN AM BODEN!");
}
}
void jumpAction ()
{
foreach (Touch touch in Input.touches) {
if (controller.isGrounded) {
canJump = true;
inAir = false;
if (touch.phase != TouchPhase.Ended && touch.phase != TouchPhase.Canceled && guiTexture.HitTest (touch.position) && canJump == true) {
inAir = true;
canJump = false;
direction = new Vector2 (0, 0);
//direction = player.transform.TransformDirection (direction);
direction.y = jumpSpeed;
if(!player.GetComponent<exSpriteAnimation>().IsPlaying("RitterSprung"))
{
player.GetComponent<exSpriteAnimation>().SetDefaultSprite();
}
if (inAir) {
if (!player.GetComponent<exSpriteAnimation> ().IsPlaying ("RitterSprung")) {
player.GetComponent<exSpriteAnimation> ().Play ("RitterSprung");
}
}
} else if (touch.phase == TouchPhase.Ended) {
if (player.GetComponent<exSpriteAnimation> ().IsPlaying ("RitterSprung"))
player.GetComponent<exSpriteAnimation> ().Stop ();
if(controller.isGrounded)
{
player.GetComponent<exSpriteAnimation>().SetDefaultSprite();
}
}
} else {
inAir = true;
}
}
}
Am i missing something? Is there a mistake somewhere? I appreciate any help :) (I'm sitting for hours in front of this problem...I