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 ninjaweirdoyt · Dec 21, 2020 at 10:03 AM · movementmovement script

How do I make my character move midair?

Can anyone help me make my character move mid air while it's jumping? I don't know how to fix this and also sometimes it randomly infinitely jumps.

using UnityEngine;

public class PlayerMovement : MonoBehaviour {

 public CharacterController2D controller;
 public Animator animator;

 public float runSpeed = 80f;

 float horizontalMove = 0f;
 bool jump = false;
 bool crouch = false;

 // Update is called once per frame
 void Update() {

     horizontalMove = Input.GetAxisRaw("Horizontal") * runSpeed;

     animator.SetFloat("Speed", Mathf.Abs(horizontalMove));

     if (Input.GetButtonDown("Jump"))
     {
         jump = true;
         animator.SetBool("IsJumping", true);
     }

     if (Input.GetButtonDown("Crouch"))
     {
         crouch = true;
     } else if (Input.GetButtonUp("Crouch"))
     {
         crouch = false;
     }

 }
 
  public void OnLanding ()
  {
     animator.SetBool("IsJumping", false);
  }

 public void OnCrouching (bool isCrouching)
 {
     animator.SetBool("IsCrouching", isCrouching);
 }

 void FixedUpdate ()
 {
     // Move our character
     controller.Move(horizontalMove * Time.fixedDeltaTime, crouch, jump);
     jump = false;
 }

} ,I don't know how to make my character move in midair and the tutorial I was watching didn't help me. Here's the code. Can anyone help me?

using UnityEngine;

public class PlayerMovement : MonoBehaviour {

 public CharacterController2D controller;
 public Animator animator;

 public float runSpeed = 80f;

 float horizontalMove = 0f;
 bool jump = false;
 bool crouch = false;

 // Update is called once per frame
 void Update() {

     horizontalMove = Input.GetAxisRaw("Horizontal") * runSpeed;

     animator.SetFloat("Speed", Mathf.Abs(horizontalMove));

     if (Input.GetButtonDown("Jump"))
     {
         jump = true;
         animator.SetBool("IsJumping", true);
     }

     if (Input.GetButtonDown("Crouch"))
     {
         crouch = true;
     } else if (Input.GetButtonUp("Crouch"))
     {
         crouch = false;
     }

 }
 
  public void OnLanding ()
  {
     animator.SetBool("IsJumping", false);
  }

 public void OnCrouching (bool isCrouching)
 {
     animator.SetBool("IsCrouching", isCrouching);
 }

 void FixedUpdate ()
 {
     // Move our character
     controller.Move(horizontalMove * Time.fixedDeltaTime, crouch, jump);
     jump = false;
 }

}

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

Answer by HicorySauce · Dec 26, 2020 at 03:24 AM

@ninjaweirdoyt What you probably want to do is clean up all those logic checks by implementing a state machine. The following page not only covers state machines but essentially implements the exact example that you are looking for: https://gameprogrammingpatterns.com/state.html . I'd highly recommend reading the whole thing and checking out more pages from that website in general.


What is most likely happening in your code is that you have horizontal movement and jumping completely separated by logic checks as being mutually exclusive - if you are jumping, you cannot move, by the specific way you've written your code. I'd suggest taking a step back from the syntax to look at the big picture of your coding process: what will happen when you add more features, like swimming, for example? When you jump, you'll have to check you're not already jumping, that you're not crouching, that you're not swimming - when you swim, you'll have to check you're crouching or jumping. If you add more character features these logic gates are just going to get worse and spiral out of control incredibly quickly. A state machine will fix this up pretty well.

Comment
Add comment · 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

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

166 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

Related Questions

Altering movement depending on camera rotation 0 Answers

Enemy shakes on Y-axis when is moving straight on the X-axis 0 Answers

How do I stop momentum in 2D sidescroller? 1 Answer

How can i add simple back and forth movement on the Y-axis to an enemy that has velocity on the X-axis attached to it? 3 Answers

How to get an enemy to move in a swooping motion?,How to get enemies to move in a swooping motion 0 Answers


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