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 hyperion2333 · Jul 31, 2019 at 12:32 PM · horizontal movement

Changing the horizontal movement direction of the player

Hello everyone,

I am using in FixedUpdate() function the Input.GetMouseButtonDown() method. I want my player to change the horizontal direction, so for this I have the following code:


 if (Input.GetMouseButtonDown(0))
         {
             if(change == true)
             {
                 rb.velocity = new Vector2(-12,rb.velocity.y);
                 change=!change;
             }
             else if(change == false)
             {
                 change=!change;
                 rb.velocity = new Vector2(12,rb.velocity.y);
             }
 }


At the beginning, first 3-6 clicks, works fine (one click change direction, another click the other direction) but afterwards I have to press 2 or 3 times to change the actual direction.

What should I do to increase the accuracy, quality of changing directions?

Thank you very much for your patience and attention!

Comment
Add comment · Show 2
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 I_Am_Err00r · Jul 31, 2019 at 12:46 PM 1
Share

Have you tried just Update() ins$$anonymous$$d of FixedUpdate()?

avatar image hyperion2333 I_Am_Err00r · Aug 06, 2019 at 09:45 AM 0
Share

Yes, that was the issue, thank you very very much sir!

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Vega4Life · Jul 31, 2019 at 03:54 PM

Like @I_Am_Err00r mentioned, you will for sure miss mouse clicks looking for input in the Fixed Update. You can put your input in Update, but leave your movement in Fixed (since you are moving via rb velocity). Something like this (I also made the direction more simplified):


     [SerializeField] float moveDistance = 12;
 
     Rigidbody rb;
     bool changeDirection;
     int currentDirection = 1;
 
 
     private void Awake()
     {
         rb = GetComponent<Rigidbody>();
     }
 
 
     private void Update()
     {
         if (Input.GetMouseButtonDown(0))
         {
             changeDirection = true;
         }
     }
 
 
     private void FixedUpdate()
     {
         if (changeDirection)
         {
             rb.velocity = new Vector3(currentDirection * moveDistance, rb.velocity.y);
             currentDirection *= -1;
             changeDirection = false;
         }
     }
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

109 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

Related Questions

Combining 2 Vectors for Diagonal Movement 1 Answer

What would be the best method by which to recreate the horizontal "swipe" mechanic of the game "Splashy!"? 2 Answers

Rotation too complicated 3 Answers

Rolling ball with Horizontal Input 1 Answer

How can I make my character jump & move him by zone 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