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 abdulhadiesalama · Aug 09, 2018 at 04:18 PM · 2d2d-physicsjumpingx-axisstops

Jumping stops x movement

Ok so im very new to coding, and i've been trying to make a movement script for my 2D player, everything is going good except for the jumping part of the script , so in the jumping part when i Jump the movement along the x Axis stops moving, so whilst im running and Jump he just stops and jumps no movement on the X axis whilst in air alt text

This is my Script

 using UnityEngine;
 
 public class Player_Movement : MonoBehaviour
 {
     public int Speed;
     private Animator PlayerAnimation;
     public bool facingRight = false;
     private float MoveX;
     public bool sprintToggle = false;
     public float JumpForce;
     public bool IsGrounded;
 
 
 
     void Start()
     {
         facingRight = false;
         sprintToggle = false;
         PlayerAnimation = GetComponent<Animator>();
         Speed = 13;
         JumpForce = 1250f;
     }
 
 
 
     private void OnCollisionEnter2D(Collision2D collision) //DETECTS IF PLAYER IS ON GROUND
     {
         if(collision.gameObject.tag == "Ground")
         {
             IsGrounded = true;
         }
     }
     private void OnCollisionExit2D(Collision2D collision) // DETECTS IF PLAYER IS OFF OF GROUND
     {
         if(collision.gameObject.tag == "Ground")
         {
             IsGrounded = false;
         }
     }
 
 
     void Update()
     {
         PlayerMove();
         Moving();
         SprintToggle();
         Jump();
     }
 
 
     public void PlayerMove() //MOVES HORIZONTALLY AND FLIPS HORIZONTALLY
     {
         if (IsGrounded == true)
         {
             MoveX = Input.GetAxis("Horizontal");
             {
                 var x = MoveX * Time.deltaTime * Speed;
                 {
 
                     transform.Translate(x, 0, 0);
 
                 }
             }
             if (MoveX < 0.0f && facingRight == true)
             {
                 FlipPlayer();
 
             }
             if (MoveX > 0.0f && facingRight == false)
             {
                 FlipPlayer();
             }
         }
     }
     public void Moving() //CHECKS IF PLAYER IS MOVING & Walking & Running Animation
     {
         if (Input.GetKey("left")&&IsGrounded == true || Input.GetKey("right") && IsGrounded == true || Input.GetKey("left") && Input.GetKey("right") && IsGrounded == true || Input.GetKey("a") && IsGrounded == true || Input.GetKey("d") && IsGrounded == true || Input.GetKey("a")&& Input.GetKey("d") && IsGrounded == true)
         {
             if (Speed <= 13)
             {
                 PlayerAnimation.SetBool("Moving", true);
                 PlayerAnimation.SetBool("Running", false);
             }
             else if (Speed > 13)
             {
                 PlayerAnimation.SetBool("Running", true);
                 PlayerAnimation.SetBool("Moving", false);
             }
             
         }
         else if (Input.GetKeyUp("left") || Input.GetKeyUp("right")|| Input.GetKeyUp("a") || Input.GetKeyUp("d"))
         {
             
             PlayerAnimation.SetBool("Moving", false);
             PlayerAnimation.SetBool("Running", false);
         }
         if (IsGrounded == false)
         {
             PlayerAnimation.SetBool("Moving", false);
             PlayerAnimation.SetBool("Running", false);
         }
     }
     public void FlipPlayer() //FLIPS PLAYER HORIZONTALLY
     {
         facingRight = !facingRight;
         Vector2 localScale = gameObject.transform.localScale;
         localScale.x *= -1;
         transform.localScale = localScale;
     }
     public void SprintToggle() //Toggles Sprint [r]
     {
         if (Input.GetKeyDown("r"))
         {
             sprintToggle = !sprintToggle;
             if (sprintToggle == false)
             {
                 Speed = 13;
             }
             else if (sprintToggle == true)
             {
                 Speed = 30;
             }
         }
     }
     public void Jump() // JUMPS
     {
         if (Input.GetKeyDown("space")&&IsGrounded == true)
         {
             GetComponent<Rigidbody2D>().AddForce(Vector2.up * JumpForce);
         }
     }
     
 }
 


jump.jpg (70.6 kB)
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by WybrenAkker · Aug 09, 2018 at 04:45 PM

Hi @abdulhadiesalama,

In your move function you have this if statement:

 if (IsGrounded == true)
          {
              MoveX = Input.GetAxis("Horizontal");
              {
                  var x = MoveX * Time.deltaTime * Speed;
                  {
  
                      transform.Translate(x, 0, 0);
  
                  }
              }
              if (MoveX < 0.0f && facingRight == true)
              {
                  FlipPlayer();
  
              }
              if (MoveX > 0.0f && facingRight == false)
              {
                  FlipPlayer();
              }
          }



Because you are using Transform.Translate to move your character will immediately stop moving once you stop calling it.

This is exactly what the above if statement is doing. When you jump your bool "IsGrounded" will be set to false. This in turn stops your Transform.Translate from being called.


If you want your player to slow down in the air over time you can do serveral things.

  1. Decrease your speed multiplier while player is in air.

  2. Move your player with physics instead. (same principal as your jump)

  3. Move your player by root motion.

Root motion comes with animations. Every animation can have root motion when you animate the root of your character to move along with its animation. This can then be used in unity to move your character. If you're interested there's loads of tutorials and documentation on it on the internet.


To fix your problem replace the above mentioned code block with:

 MoveX = Input.GetAxis("Horizontal");
 
 var x = MoveX * Time.deltaTime * Speed;
 {
       transform.Translate(x, 0, 0);
 }
              
 
 if (IsGrounded == true)
 {
       if (MoveX < 0.0f && facingRight == true)
       {
            FlipPlayer();
  
        }
        if (MoveX > 0.0f && facingRight == false)
        {
            FlipPlayer();
        }
 }

Or

     MoveX = Input.GetAxis("Horizontal");
     
     var x = MoveX * Time.deltaTime * Speed;
     {
           transform.Translate(x, 0, 0);
     }
                  
           if (MoveX < 0.0f && facingRight == true)
           {
                FlipPlayer();
      
            }
            if (MoveX > 0.0f && facingRight == false)
            {
                FlipPlayer();
            }

The first one still stops your character from rotating or flipping in air. The second one allows rotating or flipping aswell.

Hope this helps!

Wybren van den Akker

Comment
Add comment · Show 2 · Share
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
avatar image abdulhadiesalama · Aug 09, 2018 at 07:31 PM 0
Share

Thanks it worked! :)

avatar image WybrenAkker abdulhadiesalama · Aug 09, 2018 at 07:33 PM 0
Share

Great news! Good luck with your game!

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

183 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Character jumping at random heights. What in my code is causing this? 1 Answer

Make gameobject follow player on x-axis only 0 Answers

Unity 2018 2D Character Jump Glitch 1 Answer

HingeJoint2D.jointAngle flips between two values based on rotation 0 Answers

The player randomly freezes in place while other objects move ingame 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