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
-1
Question by KukiKrunch · Jan 23, 2012 at 01:45 PM · beginnerfunctionnew

function Start () problem!!

hi :)

  Im new to coding and i have this problem...

  My Goal is: If you press 'A'  the variable  subtracts 1. 
                       If you press 'D' the variable   adds 1.
                       So that you can only toggle through, "-1, 0 and 1"

                       The problem is that if I put the code inside "function Update", it will skip '0' too quickly :(  .

Heres the JavaScript... var mover : int = 0;

function Start () { if (Input.GetButton("Left")) { mover = mover -1; }

 if (Input.GetButton("Right"))
 {
     mover = mover + 1;
 }
 

}

function Update () { //this is so that it doesnt go higher than 1. if (mover > 1) { mover = 1; }

 //this is so that it doesnt go lower than -1.
 if (mover < -1)
 {
     mover = -1;
 }

}

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Felipetnh · Jan 23, 2012 at 02:34 PM

I guess the best way to do what you need is using Input.GetButtonDown().
This way you only get a true value the first frame you press the button.

var mover = 0;

function Update(){

//Here, we check for keys pressure if (Input.GetButtonDown("Left")) mover -= 1; else if (Input.GetButtonDown("Right")) mover += 1;

//Here, we check that mover is inside {-1, 0, 1} if (mover > 1) mover = 1; else if (mover < -1) mover = -1; }

Comment
Add comment · Show 2 · 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 KukiKrunch · Jan 23, 2012 at 04:00 PM 0
Share

Ok thanks ill try it now!! :)

avatar image ThePunisher · Jan 23, 2012 at 07:55 PM 0
Share

..I don't know the rules very well, but I'm pretty sure marking an answer before you try is not the best way of doing this.

avatar image
3

Answer by BiG · Jan 23, 2012 at 01:48 PM

No, KuriKrunch, don't use Start(). That function is executed only once when the game starts, and that's useless to your purpose (the keys' pressure is never checked). Try this:

var mover = 0;

function Update(){

//Here, we check for keys pressure if (Input.GetButton("Left")) mover -= 1; else if (Input.GetButton("Right")) mover += 1;

//Here, we check that mover is inside {-1, 0, 1} if (mover > 1) mover = 1; else if (mover < -1) mover = -1; }

Comment
Add comment · Show 2 · 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 KukiKrunch · Jan 23, 2012 at 04:01 PM 0
Share

O$$anonymous$$ il give a try...thanks :)

avatar image KukiKrunch · Jan 23, 2012 at 04:06 PM 0
Share

I tried your way... it works but it skips 0 if you hold the button a little too long. all it needed was "if (Input.GetButtonDown("Left"))" just like Felipetnh said...but still thanks for the help!

avatar image
0

Answer by Felipetnh · Jan 23, 2012 at 02:52 PM

Ooohh... I almost forgot... This way I told you, each time you press the button it'll add (or subtract) just one time.
So, if your "mover" variable is "-1" and you press "D", to variable will add "1" and become "0", for as long as you keep the button pressed.
If you want to add it again, you'll have to press "D" again.
Hope I could help.

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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

delete a cube with particles 0 Answers

change to glow texture on mouse click 0 Answers

On var enter 1 Answer

What am I doing wrong in this 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