Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by waffledave · Sep 27, 2016 at 10:38 AM · 2d-platformerjumping

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,

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

CS1525 unexpected symbol thisTranslate 1 Answer

Trouble with Jumping Algorithm 1 Answer

Why is my raycast projecting from the middle instead of the bottom? 0 Answers

Unity Character Not Moving Midair 2 Answers

Can someone help me with the jumping in this Player Controller script? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges