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 Joshua4missions · Aug 07, 2013 at 05:24 PM · booleanif statement

How to change boolean state based on User Input?

Hello all,

Below is a screenshot of my character movement code. It's missing one element; I need to change the boolean "xMove" based on users input.

I tried the if statement on line 19 but got the following error: "Cannot implicitly convert type float' to bool'"

So how would I go about correcting this issue?

 using UnityEngine;
 using System.Collections;
 
 public class SteveMovement : MonoBehaviour 
 {
 //Set Player Movement Speed    
 public float speed = 10.0f;    
 //Boolean to check if Horizontal movement keys are down
 
     
     void Start () {
     //Set Starting Location
     transform.position = new Vector3(5, 0, 0);
         
     }
     
     void Update () {
         
         //Move Character left and right
         //If xMove == true  --type bool
         //Move along Horizontal axis * speed * hardwareSpeed
         if (Input.GetAxis("Horizontal") > 0.5f){
             Debug.Log("Horizontal Button Is Down");
             //Execute movement along Horizontal Axis
             transform.Translate(Input.GetAxis("Horizontal") * Vector3.forward * speed * Time.deltaTime);
             //Insert animation clip via Animator
             animation.Play("Walk");
         }else{
             Debug.Log("Player is not pressing Horizontal movement button");
             animation.Play(); //Deafault animation is "Idle"
         }
         
         //Old Movement method
         //transform.Translate(Vector3.right * Input.GetAxis("Horizontal") * speed * Time.deltaTime); //Move gameObject on the X axis
         //Debug.Log("Old Horizontal Movement Initiated");
     
         
     }
     
 
 }


Thanks in advance for all help offered. :)

-Joshua4missions

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

Answer by Ejlersen · Aug 07, 2013 at 05:27 PM

 if (Input.GetButton("Horizontal"))
 {                
   transform.Translate(Vector3.right * speed * Input.GetAxis("Horizontal") * Time.deltaTime);
 
   if (!xMove)
   {
     animation.Play("Walk");
     xMove = true;
   }
 }
 else
 {
   if (xMove)
   {
     animation.Play();
     xMove = false;
   }
 }
Comment
Add comment · Show 10 · 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 Joshua4missions · Aug 07, 2013 at 05:28 PM 0
Share

What does the 0.5f mean?

avatar image Ejlersen · Aug 07, 2013 at 05:31 PM 0
Share

0.5f is a float with the value 0.5.

avatar image Joshua4missions · Aug 07, 2013 at 05:34 PM 0
Share

Okay, the project started so no compile errors that prevented it.

New development is that I can only move right, not left. No animations play as well. I guess I need to make a new topic concerning how to connect an Animator setup to code so that this doesn't go off topic. :/

avatar image Ejlersen · Aug 07, 2013 at 05:38 PM 0
Share

Paste the code and lets have a look again :)

avatar image Joshua4missions · Aug 07, 2013 at 05:40 PM 0
Share

Code is in OP. I edited the Vector3.right to Vector3.forward to fix a directional problem that developed.

Show more comments

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

15 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

Related Questions

help with boss death (bool) 1 Answer

Some Boolean "Action" Trouble 2 Answers

(C#) If statements based on variables in another script 1 Answer

I'm having trouble setting a bool. 2 Answers

2D game: When going from left input to right input fast the player stops moving 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