Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Martinx903 · Apr 20, 2021 at 03:12 PM · bug2d-platformerproblem during runtime

I have a 2D platformer runnign with a character, but randomly the Input for the Jump (space) doesn't workds.

It's not because of the collider I think, but because of the Animations, I think the jump isn't getting the input because or of the animations, that until one finish it can't go to the other, or because of the detection of the input, that it gives priority to the movement than the jump.

 {
     //Private Variables
 
     private Rigidbody2D rb2D;
     private Animator anim;
     private Collider2D coll2D;
     private bool facingLeft = true;
 
     //Inspector Variables
 
     [SerializeField] private LayerMask ground;
     [Range(0, 1)] [SerializeField] private float movementSmoothing = 0.5f;
     [SerializeField] private float jumpForce = 40f;
     [SerializeField] private float runSpeed = 4f;
     [SerializeField] private float rollForce = 40f;
     [SerializeField] private float startRollTimer;
 
     //Physics Variables
 
     private float horizontalMovement = 0f;
     private bool isJumping = false;
     private bool isRolling = false;
     private float rollDirection;
     private float currentRollTimer;
     private Vector3 velocity = Vector3.zero; 
 
     //FSM 
 
     private enum State { idle,running,jumping,falling, rolling};
     private State state = State.idle; 
 
 
     // Start is called before the first frame update
     void Start()
     {
         rb2D = GetComponent<Rigidbody2D>();
         coll2D = GetComponent<Collider2D>();
         anim = GetComponent<Animator>(); 
     }
 
     // This functin is called once per frame
     void FixedUpdate()
     {
         if(Input.GetButtonDown("Jump") && coll2D.IsTouchingLayers(ground))
         {
             isJumping = true;
         }
         else
         {
             isJumping = false;
         }
         if (Input.GetButtonDown("Fire1") && coll2D.IsTouchingLayers(ground))
         {
 
             isRolling = true;
             currentRollTimer = startRollTimer;
             rb2D.velocity = Vector2.zero;
             rollDirection = (int)horizontalMovement;
             state = State.rolling;
 
         }
         //Detectamos la velocidad del jugador en base a si calca A o D y le pasamos al código un número entre -1 y 1.
         horizontalMovement = Input.GetAxisRaw("Horizontal") * runSpeed;
         Movement();
         AnimationController(); 
         anim.SetInteger("state", (int)state); 
     }
 
     void Movement()
     {
 
         if (isJumping)
         {
             //Actualizamos su estado a saltando
 
             state = State.jumping;
 
             //Aplicamos una fuerza vertical que le lance al aire
 
             rb2D.AddForce(new Vector2(0f, jumpForce));
 
         }
 
         if (isRolling)
         {
             rb2D.velocity = transform.right * rollDirection * rollForce;
 
             currentRollTimer -= Time.fixedDeltaTime; 
 
             if(currentRollTimer <= 0)
             {
                 isRolling = false; 
             }
 
         }
 
         //Cogemos la velocidad del personaje en base al input que te da Unity por los axis.
         Vector3 targetVelocity = new Vector2(horizontalMovement * Time.fixedDeltaTime * 10f, rb2D.velocity.y);
         //La smootheamos con esta función
         rb2D.velocity = Vector3.SmoothDamp(rb2D.velocity, targetVelocity, ref velocity, movementSmoothing);
 
         if (horizontalMovement > 0 && !facingLeft)
         {
             Flip();
         }
         else if (horizontalMovement <0 && facingLeft)
         {
             Flip();
         }
     }
 
     void Flip()
     {
         //Cambiamos la forma en la que le pasamos al programa la dirección del personaje
 
         facingLeft = !facingLeft;
 
         //Multiplicamos la escala del personaje por 1 o -1 en base a si está mirando hacia la izq o no
 
         Vector3 scale = transform.localScale;
         scale.x *= -1;
         transform.localScale = scale; 
     }
 
     void AnimationController()
     {
         if(state == State.jumping)
         {
             if(rb2D.velocity.y < -.1f)
             {
                 state = State.falling;
             }
         }
         else if(state == State.falling)
         {
             if(coll2D.IsTouchingLayers(ground))
             {
                 state = State.idle;
             }
         }
         else if(state == State.rolling)
         {
             if(currentRollTimer <= 0 && coll2D.IsTouchingLayers(ground))
             {
                 state = State.idle; 
             }
         }
         else if ((Mathf.Abs(horizontalMovement) > 0.2f) && coll2D.IsTouchingLayers(ground))
         {
             state = State.running;
         }
         else
         {
             state = State.idle; 
         }
     }
 
 
 }
 
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

146 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

Related Questions

How do I stop the player from gliding while in the air? 1 Answer

Each click in Unity opens a window with the text “code compilation”. 0 Answers

black screen over project window 0 Answers

Unity Colors become weird when scene changed 1 Answer

Unity Crashes Every time I Try To Run This Code 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