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 Steeban · Dec 26, 2015 at 05:03 AM · controllerenuminputmanagergetaxis

What is the purpose of the following Script?

Hi, I am following a Lynda course(Advance 2D Platformer). In the first lesson, they are creating a custom made input manager. I attached the code. My questions are, What is the main purpose of using this? Can't we use the simple method (Input.GetAxis())? Explain me please. I just learned some basic stuffs and moved on to this and this is just confusing me. Sorry for my poor English.

InputState.cs

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class ButtonState{
     public bool value;
     public float holdTime=0;
 }
 
 public class InputStates : MonoBehaviour {
 
     private Dictionary<Buttons, ButtonState> buttonStates = new Dictionary<Buttons,ButtonState>();
 
     public void SetButtonValue(Buttons key, bool value){
 
         if(!buttonStates.ContainsKey(key))
             buttonStates.Add(key,new ButtonState());
 
         var state = buttonStates [key];
 
         if(state.value && !value){
             Debug.Log("Button "+key+ " release "+ state.holdTime);
             state.holdTime = 0;
         }else if(state.value && value){
             state.holdTime += Time.deltaTime;
 
             Debug.Log("Button "+key+ " down "+ state.holdTime);
         }
 
         state.value = value;
     }
 
     public bool GetButtonValue(Buttons key){
         if(buttonStates.ContainsKey(key))
             return buttonStates[key].value;
         else
             return false;
     }

InputManager

     using UnityEngine;
 using System.Collections;
 
 public enum Buttons{
     Right,
     Left,
 }
 
 public enum Conditions{
     GreaterThan,
     LessThan
 }
 
 [System.Serializable]
 public class InputAxisState{
     public string axisName;
     public float offValue;
     public Buttons button;
     public Conditions condition;
 
     public bool value{
 
         get{
             var val = Input.GetAxis(axisName);
 
             switch(condition){
             case Conditions.GreaterThan:
                 return val > offValue;
             case Conditions.LessThan:
                 return val < offValue;
             }
 
             return false;
 
         }
     }
 
 }
 
 
 public class InputManager : MonoBehaviour {
 
     public InputAxisState[] inputs;
     public InputStates inputState;
 
     // Use this for initialization
     void Start () {
 
     }
 
     // Update is called once per frame
     void Update () {
         foreach(var input in inputs){
             inputState.SetButtonValue(input.button, input.value);
         }
     }
 } 

SimpleMovement.cs

 using UnityEngine;
 using System.Collections;
 
 public class SImpleMovement : MonoBehaviour {
 
     public float speed;
     public Buttons[] input;
 
     private Rigidbody2D body2D;
     private InputStates inputState;
 
     // Use this for initialization
     void Start () {
         body2D = GetComponent<Rigidbody2D>();
         inputState = GetComponent<InputStates>();
     }
 
     // Update is called once per frame
     void Update () {
 
         var right = inputState.GetButtonValue(input[0]);
         var left = inputState.GetButtonValue(input[1]);
         var velX = speed;
 
         if(right || left){
             velX *= left ? -1 : 1;
         }else{
             velX = 0;
         }
 
         body2D.velocity = new Vector2(velX, body2D.velocity.y);
 
     }
 }

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

Answer by allenallenallen · Dec 26, 2015 at 05:10 AM

Well, the script's purpose is exactly the title of your question: Input Manager.

I think the reason they used a custom made Input Manager is so that it allows users to change the buttons for the controls later in the options menu.

I'm pretty sure that's the only reason. I don't have this course on Lynda so I can't tell for sure but it seems like the author Jesse Freeman wants to have more control than the default Unity Input Manager.

It also looks like he added multiplayer co-op inputs so I guess the way he does his own Input Manager is better suited for this game's purpose or something.

All in all, the only reason you'd want to make another Input Manager from scratch when there's already one built within Unity is so that you can have more overall control.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Can't get Triggers to Work [Solved] 1 Answer

Controller shoulder buttons firing both at the same time. 0 Answers

XBox controller does not have a name 1 Answer

Flight code not reading throttle axis 1 Answer

Editing the StandAloneInputModule 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