- Home /
2D platformer: Running and jumping at the same time only works in one direction
Hi everyone,
New to Unity and completing some exercises for my class. They gave up this task during a tutorial and there's one thing I just can't figure out.
This is basically a Mario style platformer where we need to make him walk, run and jump.
Everything is working perfectly except for one thing. He can walk side to side correctly and jump. He can run to the left and jump. But when I try to run to the right, he simply will not jump.
Here is my C# script:
using UnityEngine;
using System.Collections;
public class Mario : MonoBehaviour
{
// Variables set in the inspector
[SerializeField]
float mWalkSpeed;
[SerializeField]
float mRunSpeed;
[SerializeField]
float mJumpForce;
[SerializeField]
LayerMask mWhatIsGround;
float kGroundCheckRadius = 0.1f;
// Booleans used to coordinate with the animator's state machine
bool mRunning;
bool mMoving;
bool mGrounded;
bool mFalling;
// References to other components (can be from other game objects!)
Animator mAnimator;
Rigidbody2D mRigidBody2D;
Transform mSpriteChild;
Transform mGroundCheck;
Vector2 direction;
void Start ()
{
// Get references to other components and game objects
mAnimator = GetComponent<Animator>();
mRigidBody2D = GetComponent<Rigidbody2D>();
mSpriteChild = transform.FindChild ("MarioSprite");
mGroundCheck = transform.FindChild ("GroundCheck");
}
void Update ()
{
CheckGrounded ();
MoveCharacter ();
CheckFalling ();
// Update animator's variables
mAnimator.SetBool("isRunning", mRunning);
mAnimator.SetBool("isMoving", mMoving);
// TODO: Tell animator if game object is grounded or not (use the variable "mGrounded")
mAnimator.SetBool("isGrounded", mGrounded);
// TODO: Tell animator if game object is falling or not (use the variable "mFalling")
mAnimator.SetBool("isFalling", mFalling);
}
private void CheckGrounded()
{
Collider2D[] colliders = Physics2D.OverlapCircleAll(mGroundCheck.position, kGroundCheckRadius, mWhatIsGround);
foreach(Collider2D col in colliders)
{
if(col.gameObject != gameObject)
{
mGrounded = true;
return;
}
}
mGrounded = false;
}
private void MoveCharacter()
{
// TODO: Check if the player wants Mario to run (see input manager)
// and set the value of "mRunning" accordingly
if (Input.GetKey(KeyCode.LeftShift))
{
mRunning = true;
mRunSpeed = 5;
mWalkSpeed = 1;
}
else
{
mRunning = false;
mRunSpeed = 1;
mWalkSpeed = 2;
}
// TODO: Make Mario move when the player presses Left or Right!
// Also, move Mario walk/run at the appropriate speed.
// Use the variables "mWalkSpeed" and "mRunSpeed"!
// Don't forget to flip Mario when necessary (use the FaceDirection() function)
if (Input.GetKey(KeyCode.RightArrow))
{
mMoving = true;
direction.x = Input.GetAxis("Right");
Vector2 move = new Vector2(direction.x, 0);
FaceDirection(move);
transform.Translate(move * mWalkSpeed * mRunSpeed* Time.deltaTime);
}
else if (Input.GetKey(KeyCode.LeftArrow))
{
mMoving = true;
direction.x = Input.GetAxis("Left");
Vector2 move = new Vector2(-direction.x, 0);
FaceDirection(move);
transform.Translate(move * mWalkSpeed * mRunSpeed * Time.deltaTime);
}
else
mMoving = false;
// TODO: If Mario is on the ground, allow him to jump!
// Make use of the "mGrounded" variable, whose value is being changed by the CheckGrounded() function
if (Input.GetKeyDown(KeyCode.Space) && mGrounded)
{
mRigidBody2D.AddForce(Vector2.up * mJumpForce);
}
}
private void CheckFalling()
{
mFalling = mRigidBody2D.velocity.y < 0.0f;
}
private void FaceDirection(Vector2 direction)
{
// Flip the sprite (NOTE: Vector3.forward is positive Z in 3D. The Sprite is on XY plane!)
Quaternion rotation3D = direction == Vector2.right ? Quaternion.LookRotation(Vector3.forward) : Quaternion.LookRotation(Vector3.back);
mSpriteChild.rotation = rotation3D;
}
}
I checked to see if somehow he is not "grounded" when running to the right but this is not the problem. It seems odd to me that when he is walking left or right he can jump without issue. And he can run to the left and jump. It just seems that running to the right doesn't allow him to jump at all, he just stays on the ground.
I thought perhaps it had something to do with the rotation, maybe trying to make him jump "down" but this doesn't make sense, since it works fine when he is walking. I'm sure I am missing something simple but I just don't see it!
Thanks,