Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 tonycoculuzzi · Jun 07, 2012 at 07:40 AM · inputaxisgetaxisartificial

Is there a way to manually change the Input.Axis values?

I'm making a virtual controller, and I was wondering if I could manually change an Input.Axis's value. For example, when a game object is at a certain location, I want to be able to change the axis("horizontal") value to 1 or -1, without the help of hardware.

From what I've seen, I can only call GetAxis(name), but I'd like something like SetAxis(name, x). Is there any way to do this?

Comment
Add comment · Show 2
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 rutter · Jun 07, 2012 at 08:08 AM 1
Share

Not as such, but you could write your own input manager which wraps around Unity's input classes and provides the functionality you're looking for. For example, reading from an input axis normally except for when you set some override value.

One thing you'll want to clarify, if you go that route: how long does this override last for? Some amount of time? Until some in-game event?

Depending on the exact features you're looking for, that can be fairly quick or extremely involved. I've found that most complex projects wind up doing at least some input wrapping, sooner or later.

avatar image tonycoculuzzi · Jun 07, 2012 at 08:19 AM 0
Share

I figured as much. That's unfortunate, I believe the Input class could be much more powerful if it allowed for manual value entries.

That said, what I've done is just create a CustomInput class that allows for setting and getting an input value. It's not as in-depth as the current Input class, but it works just as I'd like it to. :)

2 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by tonycoculuzzi · Jun 07, 2012 at 08:23 AM

To answer my own question, it appears that you can not set custom values in the Input class. What I ended up doing was writing my own CustomInput class that allows for setting and getting input values.

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public static class CustomInput {
     static Dictionary<string,float> inputs = new Dictionary<string, float>();
     
     static public float GetAxis(string _axis){
         if(!inputs.ContainsKey(_axis)){
             inputs.Add(_axis, 0);
         }
         
         return inputs[_axis];
     }
     
     static public void SetAxis(string _axis, float _value){
         if(!inputs.ContainsKey(_axis)){
             inputs.Add(_axis, 0);
         }
         
         inputs[_axis] = _value;
     }
 }

All values are automatically added if they don't already exist. Use CustomInput.GetAxis(name) to get, and CustomInput.SetAxis(name, value) to set.

The only problem is, I'm not quite sure how optimized this will be for mobile, but feel free to use it anyways.

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 Deepscorn · Oct 04, 2015 at 07:26 PM 1
Share

Another alternative - use CrossPlatformInput$$anonymous$$anager from Standart assets. See joystick script there as an example. To get values use: CrossPlatformInput$$anonymous$$anager.GetAxis(name) CrossPlatformInput$$anonymous$$anager.GetAxisRaw(name)

avatar image golay · Jun 28, 2020 at 02:52 PM 0
Share

I have a certain use case wherein I could use your CustomInput class to get and set the axis value. The only caveat is that I'm using a library (Ros#) which uses Input.GetAxis(axisName) in some of their scripts. How would I expose the CustomInput to the Unity's own Input so that I could use this functionality without having to change much of the code in the library?

PS. I don't have much experience in Unity and I'm just beginning to learn concepts of Unity!

avatar image
1

Answer by RandomAndrei · Jul 12, 2019 at 04:00 AM

I've found another way - you can set input.GetAxis to a float and then change it's value.

private float horizontalInput;

void Update() { horizontalInput = Input.GetAxis("Horizontal");

 if (horizontalInput > 0) {// Move right}
 // Same as
 if (Input.GetAxis("Horizontal") > 0) { // Move right}}


That means you can change the horizontalInput if you want to but otherwise it will be equal to Input.GetAxis("Horizontal") - for example you can set it to a certain value if it meets a condition:

void Update() {

 if (!shouldMove) {
 horizontalInput = 0;

} else { horizontalInput = Input.GetAxis("Horizontal"); }

}

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 ahsen35813 · Apr 12, 2021 at 11:09 PM 0
Share

That's actually a really good solution that's so obvious that it's unintuitive.

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

7 People are following this question.

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

Related Questions

New Input System How to get Left Stick Axis 1 Answer

Steering Wheel / joystick GetAxis trouble 0 Answers

How to make new Axis? 2 Answers

Creating an input manager that supports axis 0 Answers

How to get an axis without defining it in the InputManager 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