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 starsuser · Jul 28, 2018 at 11:06 AM · if-statementstranslateiftransform.translate

Translate doesnt work inside IF statement, but does outside.

Hi I have a code for my player to move if you push two buttons. If both statements are true (both buttons are pushed). then the player should move. However it doesn't. the weird thing is that it does when it's outside the IF-statement.

 // Update is called once per frame
     void FixedUpdate () {
        
          // the player does move when this is uncommented:
         //transform.Translate(0f, -1f * Time.deltaTime, 0f);
 
         if (Button1down == 1&& Button2down == 1) 
         {
             //player must move
             transform.Translate(0f, -1f * Time.deltaTime, 0f);
         }
 
     }
Comment
Add comment · Show 9
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 dan_wipf · Jul 28, 2018 at 11:31 AM 0
Share

is button1down a boolean? if yes then it should be if(button1down == true && button2down == true) {... } or short if(button1down&&button2down) {... }

avatar image starsuser dan_wipf · Jul 28, 2018 at 11:50 AM 0
Share

thanks, but no I made it as an integer. and by the way if I put in print("lets see if this works.."); in the IF statement it does show up. but the transform doesnt work...

avatar image anurag7991 starsuser · Jul 28, 2018 at 12:03 PM 0
Share

// the player does move when this is uncommented: //transform.Translate(0f, -1f * Time.deltaTime, 0f);

   if (Button1down == 1 && Button2down == 1) 
   {
       //player must move
       transform.Translate(0f, -1f * Time.deltaTime, 0f);
   }
 

} maybe try adding a space b/w the button1down == 1 && ins$$anonymous$$d of 1&& maybe that is causing problem

Show more comments
avatar image starsuser · Jul 28, 2018 at 06:10 PM 0
Share

okay, and by the way. I noticed when I check the Button1- and Button2down manually in the inspector, it does work. alt text

cutscreen1.png (11.4 kB)
avatar image starsuser · Jul 28, 2018 at 06:32 PM 0
Share

okay, so with the script applied to an empty gameobject it will check the Buttondown. but not on my player. Does anyone knows what might be the case? I am new at this, so it might be something very obvious. alt text

cutscreen2.png (35.0 kB)

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by anurag7991 · Jul 28, 2018 at 12:01 PM

void FixedUpdate () {

       // the player does move when this is uncommented:
      //transform.Translate(0f, -1f * Time.deltaTime, 0f);
 
      if (Button1down == 1 && Button2down == 1) 
      {
          //player must move
          transform.Translate(0f, -1f * Time.deltaTime, 0f);
      }
 
  }

maybe try adding a space b/w the button1down == 1 && instead of 1&& maybe that is causing problem

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 starsuser · Jul 28, 2018 at 03:11 PM 0
Share

thank you for your answer. That was a sloppiness from my part to copy my code to here, but not the problem.

avatar image
0

Answer by Nebukam · Jul 28, 2018 at 03:24 PM

Make sure Button1down & Button2down aren't == 1 for one frame only, the log inside the if may be misleading as it will trigger once, and so will the Translate, but given you're using Time.deltaTime, for the duration of one frame (e.g 0.001 something) the Translate won't be nitceable. Check the transform in the editor while running play mode, it's highly likely that your if state is only valid once.

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 starsuser · Jul 28, 2018 at 03:50 PM 0
Share

thank you for you answer, but I think it does run for multiple frames. I have a comment in the IF loop saying "player moves". I added a screenshot, where you can see it pushes out in an endless stream. I am just started with Unity, so I don't know for sure.

what I think is weird, is that like I said the player does move if the code is out the IF loop, and not if its in. Although it (seems) it will push a normal print in the IF loop just fine.

alt text

cutscreen.png (54.5 kB)

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

90 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

Related Questions

Input.GetKey(KeyCode.E) requires multiple presses. 1 Answer

Decreasing a value within the if statement ! 0 Answers

Removing The Slide From Transform.Translate 1 Answer

Move terrain towards object? (illusion of moving forward) 1 Answer

play an audio clip in an if statement 2 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