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
0
Question by TisMOTH · Feb 23 at 12:19 AM · controlleraxisjoystickplayer movementcontrols

How do I create joystick controls with two fixed speeds? (3D)

I'm trying to make joystick controls that have two fixed speeds, walk and run, with no sensitivity. The problem I'm encountering is that without directly using the axis input, the player's rotation becomes stiff as though using WASD, which is exactly what I don't want. My current workaround is to determine which input is greater and set it to a fixed speed while the other is input sensitive. This creates the illusion of fixed speed, but it still doesn't do what I want. (The stiff movement still occurs when walking.) Here's the code I have right now:

 totInput = Mathf.Sqrt(hInput * hInput + fInput * fInput);

 // Determine Speed
 if (totInput > 0.7f)
     speed = speedFast;
 else
     speed = speedSlow;
 
 // Horizontal Movement
 if (hInput != 0)
 {
     if (Mathf.Abs(fInput) >= Mathf.Abs(hInput))
         transform.Translate(Vector3.right * speed * hInput * Time.deltaTime);
     else
         transform.Translate(Vector3.right * speed * hSign * Time.deltaTime);
 }
 
 // Forward Movement
 if (fInput != 0)
 {
     if (Mathf.Abs(fInput) <= Mathf.Abs(hInput))
         transform.Translate(Vector3.forward * speed * fInput * Time.deltaTime);
     else
         transform.Translate(Vector3.forward * speed * fSign * Time.deltaTime);
 }


(As a note, I'm currently using a rigidbody but I want to switch to using a character controller. Also, sorry if my code is a little wack. I'm new to Unity and I'm transitioning from java to C#.)

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 Master109 · Feb 23 at 01:59 AM

Maybe I am misunderstanding the question but to avoid stiff rotation like WASD set the player's rotation using:

 transform.rotation = Quaternion.LookRotation(new Vector3(hInput, 0, fInput), Vector3.up);
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 TisMOTH · Feb 23 at 03:12 AM 0
Share

I attempted to use this in my code, but I couldn't figure out how to use it to solve my problem. For reference, this is what I tested in a sandbox script: move = new Vector3(hInput, 0, fInput); if (move != Vector3.zero) transform.rotation = Quaternion.LookRotation(move, Vector3.up); controller.SimpleMove(move * speed); Still, the translation is determined by the input sensitivity (i.e. slightly tilting the joystick results in a crawling speed, whereas I want it to always be the walking speed). Is there another way to approach this using rotation instead of translating the xyz positions?

avatar image Master109 TisMOTH · Feb 23 at 03:30 AM 1
Share

Ah, I get the problem now. Add ".normalized" after move to make the length of the vector equal to 1: controller.SimpleMove(move.normalized * speed);

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

148 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

Related Questions

How to zero out the value of the joystick 0 Answers

PS4 Controller Y-axis is linked to L2-button? 0 Answers

How do I map joystick input to run method 0 Answers

How to know if finger is on joystick if it is at horizontal & vertical zero 0 Answers

How do I add input for two different local players? 1 Answer


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