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 /
  • Help Room /
avatar image
1
Question by darklink1433 · Oct 03, 2016 at 11:59 PM · keycodegetkeydownbooleans

can you set boolean values using the input.getkeydown(keycode.(key))

hello, I am fairly new to this, and didn't find another forum post for this, though I may not have done a query well enough to find the correct post. My code is below (found these through various searches).

 using UnityEngine;
 using System.Collections;
 public float playerSpeed;
 public float playerSpeedInitial = 1.5f;
 public float playerSpeedMax = 10f;
 public bool moving;
 private bool moveForward;
 private bool moveRight;
 private bool moveBackward;
 private bool moveLeft;
 public class Movement : MonoBehaviour` {
 
 
     // Use this for initialization
     void Start () {
         
 
     }
     
     // Update is called once per frame
     void Update () {
         while (moving && playerSpeed != playerSpeedMax){
             playerSpeed = playerSpeedInitial + .15f * Time.deltaTime;
         }
 
         while (moving && playerSpeed = playerSpeedMax){
             playerSpeed = playerSpeedMax;
         }
           // below is the part that seems to be causing an issue, so I just wanted to
         //check if this would work... so basically, can this input change the Boolean 
      //value of moveForward, and if it can, do I have to modify this to make it work as 
    //it should? (cant remember how to make multi line comments, so sorry about that.)

         if (Input.GetKeyDown(KeyCode.W)){
             moveForward = true;
         }
         if (moveForward){
             transform.localPosition += transform.forward * playerSpeed;
             moving = true;
         }
         if (Input.GetKeyDown(KeyCode.D)){
             moveRight = true;
         }
         if (moveRight){
             transform.localPosition += transform.right * playerSpeed;
             moving = true;
         }
         if (Input.GetKeyDown(KeyCode.S)){
             moveBackward = true;
         }
         if (moveBackward){
             transform.localPosition += transform.backward * playerSpeed;
             moving = true;
         }
         if (Input.GetKeyDown(KeyCode.A)){
             moveLeft = true;
         }
         if (moveLeft){
             transform.localPosition += transform.left * playerSpeed;
             moving = true;
         }
         if !moving{
             playerSpeed = 0f;
         }
 
         }
     }
 }
 




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
Best Answer

Answer by TBruce · Oct 04, 2016 at 12:04 AM

You can do this (forgive me for messing up your braces)

 using UnityEngine;
 using System.Collections;
 public class Movement : MonoBehaviour
 {
     public float playerSpeed;
     public float playerSpeedInitial = 1.5f;
     public float playerSpeedMax = 10f;
     public bool moving;
     private bool moveForward;
     private bool moveRight;
     private bool moveBackward;
     private bool moveLeft;
 
     // Use this for initialization
     void Start ()
     {
 
     }
 
     // Update is called once per frame
     void Update ()
     {
 /*
         // the following could be an endless loop - uncomment it at your own risk (save scene first
         while (moving && playerSpeed != playerSpeedMax)
         {
             playerSpeed = playerSpeedInitial + .15f * Time.deltaTime;
         }
 */
 
 /*
         // the following is an endless loop - un comment it at your own risk (save scene first
         while (moving && playerSpeed == playerSpeedMax)
         {
             playerSpeed = playerSpeedMax;
         }
     }
 */
 
         moveForward = (Input.GetKeyDown(KeyCode.W));
         moveRight = (Input.GetKeyDown(KeyCode.D));
         moveBackward = (Input.GetKeyDown(KeyCode.S));
         moveLeft = (Input.GetKeyDown(KeyCode.A));
         moving = (moveForward || moveRight || moveBackward || moveLeft);
 
         if (moveForward)
         {
             transform.localPosition += transform.forward * playerSpeed;
         }
         if (moveRight)
         {
             transform.localPosition += transform.right * playerSpeed;
         }
         if (moveBackward)
         {
             transform.localPosition += transform.backward * playerSpeed;
         }
         if (moveLeft)
         {
             transform.localPosition += transform.left * playerSpeed;
         }
 
         if (!moving)
         {
             playerSpeed = 0f;
         }
     }
 }

or you can do this

 using UnityEngine;
 using System.Collections;
 public class Movement : MonoBehaviour
 {
     public float playerSpeed;
     public float playerSpeedInitial = 1.5f;
     public float playerSpeedMax = 10f;
     public bool moving;
     private bool moveForward;
     private bool moveRight;
     private bool moveBackward;
     private bool moveLeft;
 
     // Use this for initialization
     void Start ()
     {
 
     }
 
     public void OnMouseDown  ()
     {
         moveForward = (Input.GetKeyDown(KeyCode.W));
         moveRight = (Input.GetKeyDown(KeyCode.D));
         moveBackward = (Input.GetKeyDown(KeyCode.S));
         moveLeft = (Input.GetKeyDown(KeyCode.A));
         moving = (moveForward || moveRight || moveBackward || moveLeft);
 
         if (moveForward)
         {
             transform.localPosition += transform.forward * playerSpeed;
         }
         if (moveRight)
         {
             transform.localPosition += transform.right * playerSpeed;
         }
         if (moveBackward)
         {
             transform.localPosition += transform.backward * playerSpeed;
         }
         if (moveLeft)
         {
             transform.localPosition += transform.left * playerSpeed;
         }
 
         if (!moving)
         {
             playerSpeed = 0f;
         }
     }
 
     // Update is called once per frame
     void Update ()
     {
 /*
         // the following could be an endless loop - uncomment it at your own risk (save scene first
         while (moving && playerSpeed != playerSpeedMax)
         {
             playerSpeed = playerSpeedInitial + .15f * Time.deltaTime;
         }
 */
 
 /*
         // the following is an endless loop - uncomment it at your own risk (save scene first
         while (moving && playerSpeed == playerSpeedMax)
         {
             playerSpeed = playerSpeedMax;
         }
     }
 */
 }

First of you had this for one of your while loops

 while (moving && playerSpeed = playerSpeedMax)

this

 playerSpeed = playerSpeedMax

is an assignment, not a comparison. You either wanted this

 playerSpeed != playerSpeedMax

or this

 playerSpeed == playerSpeedMax

Second, both while loops have the potential to be endless loops. I commented them out. You should only uncomment them (one at a time) after saving your scene.

Comment
Add comment · Show 3 · 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 TBruce · Oct 04, 2016 at 01:23 AM 1
Share

@darklink1433 Did you find this answer helpful?

avatar image darklink1433 · Oct 04, 2016 at 01:37 AM 0
Share

yes, It did, ill try these, thanks very much for the help!

avatar image TBruce darklink1433 · Oct 04, 2016 at 01:41 AM 1
Share

Could you please be so kind as to click the "Accept" button above to accept the answer if you have found this helpful?

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Other way to write a code 0 Answers

trouble switching between booleans 2 Answers

Change button after 'x' amount of time 2 Answers

Using one key only, change bool to true or false depending on it's current value 2 Answers

Turn on Prefab with Keycode issue 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