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 airbagnr1 · Jun 12, 2017 at 01:37 PM · c#controllerjoysticksensitivity

Controller Joystick Sensitivity

Hi there, Im currently adapting my code so that when the player moves the joystick a little bit then the player will move slow but when they push the joystick all the way then they will go to max speed. However i dont really know how to do this.... i read it has something to do with "GetAxisRaw" however im all ready doing this. This is the code (c#) for the movement just now which when the player uses the joystick at any force they still move at max speed: //Force for x and y values float forceX = 0f; float forceY = 0f; //Vel equals absolute velocity float vel = Mathf.Abs(myBody.velocity.x); //H equals moving left/right float h = Input.GetAxisRaw("Horizontal"); //If h is greator than 0 if(h >0) { //if vel is less than maxvelocity if (vel < maxVelocity) { //If player is on the ground if (grounded) { GetComponent<AudioSource>().Play(); //moving along x axis is eqal to moveforce forceX = moveForce; print(vel); } else { //Gives more control in air, force is equal to move force times 1.1 forceX = moveForce * 1.1f; } } Vector3 scale = transform.localScale; scale.x = 1f; transform.localScale = scale; anim.SetBool("DetectiveRun", true); } else if(h<0) { if (vel < maxVelocity) { if (grounded) { GetComponent<AudioSource>().Play(); forceX = -moveForce; print(vel); } else { forceX = -moveForce * 1.1f; } } Vector3 scale = transform.localScale; scale.x = -1f; transform.localScale = scale; anim.SetBool("DetectiveRun", true); } else { anim.SetBool("DetectiveRun", false); }
``Any help would be great! Thanks!

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 Kishotta · Jun 12, 2017 at 03:28 PM

There are a couple of ways I can see you can eliminate some if statements. One way is to get a multiplier for your velocity that indicates direction using a ternary statement. Something like:

 int direction = h > 0 ? 1 : -1; // direction = 1 for h > 0 and -1 for h < 0

Now to change direction you can simply do forceX = direction * moveForce and transform.localScale = new Vector3 (1, direction, 1);. No if statements required.

As for getting a "walk zone" on your input, typically you would set up two thresholds: a DeadzoneThreshold and a WalkThreshold. You might set these to 0.2 and 0.75 respectively (tweak them to get the feel you want).

These could be used to do something like this:

 float hInput = Input.GetAxisRaw ("Horizontal"); // No smoothing on input
 int direction = hInput > 0 ? 1 : -1; // Direction of input
 hInput = Mathf.Abs (hInput); // We only care about magnitude now
 if (hInput < DeadzoneThreshold) { // Input isn't significant enough to register
     // Do nothing
 } else if (hInput < WalkThreshold) { // Just enough to walk
     if (grounded) {
         forceX = direction * walkForce;
     } else {
         // Can't walk in air?
     }
 } else { // Enough to run
     if (grounded) {
         forceX = direction * runForce;
     } else {
         forceX = direction * runForce * 1.1f; // Air movement
     }
 }

It looks like you might want to read up on Finite State Machines and the State pattern if you plan on adding many more actions to your player, but you're on the right track.

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 airbagnr1 · Jun 20, 2017 at 11:53 AM 0
Share

Thanks man, much appreciated! Your a legend!

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

327 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Input not recognized after swapping control devices,Input being ignored (New Input System) 0 Answers

How can I rotate my player to look at the direction my Joystick is pointing to? (Top-Down 2D) 0 Answers

Multiple Cars not working 1 Answer

C# Clamp joystick rotation 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