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 daniel14657 · Aug 31, 2020 at 10:33 PM · rigidbodyinput

Vertical input won't work without Horizontal

Hey everyone, I have a problem when I try to move my character in the vertical axis it won't work unless I give a Horizontal input, Does someone have an idea why is it like that? Here is my code in case you need that. Thanks for helping :) {

 public float PlayerMovementSpeedX = 1f;
 public float PlayerMovementSpeedY = 1f;


 // Start is called before the first frame update
 void Start()
 {
     p_t = GetComponent<Transform>();
     p_rb = GetComponent<Rigidbody>();
     p_a = GetComponent<Animator>();
 }


 private void FixedUpdate()
 {
     float PlayerMovementY = Input.GetAxis("Vertical");
     float PlayerMovementX = Input.GetAxis("Horizontal");
     

     Vector2 Movement = new Vector2(PlayerMovementX, PlayerMovementY);

     if (Movement.x == 0)
     {
         p_a.Play("Idle");
     }

     else if(Movement.y !=0 | Movement.x != 0)
     {
         p_rb.velocity = new Vector2(PlayerMovementX * PlayerMovementSpeedX, PlayerMovementY * PlayerMovementSpeedY);
         p_a.Play("Walk");
     }

 
     //Event to call Flip functione
     if(FacingRight == true & Movement.x < 0 | FacingRight == false & Movement.x > 0)
     {
         flip();
         FacingRight = !FacingRight;
     }
 }
 void flip()
 {
     Vector2 Scale = new Vector2 (transform.localScale.x * -1, transform.localScale.y);
     transform.localScale = Scale;
 }

}

Thanks for helping ;)

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 daniel14657 · Sep 01, 2020 at 10:50 AM

Hey, Rookie mistake, After my first "if" I did "else if" which mean if only X is not equal to zero, then continue to the next "else if", Just had to change the "else if" to "if". Thanks for helping

Comment
Add comment · Show 1 · 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 ShadyProductions · Sep 01, 2020 at 11:11 AM 0
Share

I already see another issue, you retrieve input in FixedUpdate instead of Update, this makes it so that you won't retrieve input on each frame, but only when fixed update runs. This makes movement unresponsive or jerky.

 private float Player$$anonymous$$ovementX, Player$$anonymous$$ovementY;
 
 private void Update()
 {
      Player$$anonymous$$ovementY = Input.GetAxis("Vertical");
      Player$$anonymous$$ovementX = Input.GetAxis("Horizontal");
 }
 
 private void FixedUpdate()
 {
     // Do logic to Player$$anonymous$$ovementX and Y here
 }

This is better.

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

206 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Touching Falling Objects 1 Answer

AddTorque doesn't rotate? 3 Answers

Making a ship move 1 Answer

How do i get the cube to turn when up and right or left arrow is pressed? 1 Answer

Jump Input Sensitivity 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