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 Amberlewis012 · Aug 20, 2020 at 03:52 PM · movementcontrollerjoystickcontrol

How to set joystick for movement

Hello, how can I set a joystick from those on the asset store or just normal buttons to replace these actions on a mobile device? I've been struggling to integrate them and I'm quite new to all this as well. Also, if you have any ideas on how to make this code better, then hope you can help me as well. Thanks a lot!

 public Rigidbody rb;
 public float moveForce = 5f;
    
 void Update()
 {
     if (Input.GetKey("d") || (Input.GetKey(KeyCode.RightArrow)))
     {
         rb.AddForce(moveForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
     }
     if (Input.GetKey("a") || (Input.GetKey(KeyCode.LeftArrow)))
     {
         rb.AddForce(-moveForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
     }
     if (Input.GetKey("w") || (Input.GetKey(KeyCode.UpArrow)))
     {
         rb.AddForce(0, 0, moveForce * Time.deltaTime, ForceMode.VelocityChange);
     }
     if (Input.GetKey("s") || (Input.GetKey(KeyCode.DownArrow)))
     {
         rb.AddForce(0, 0, -moveForce * Time.deltaTime, ForceMode.VelocityChange);
     }
     if (Input.GetKey("e"))
     {
         transform.Rotate(Vector3.up * speed * Time.deltaTime);
     }
     if (Input.GetKey("q"))
     {
         transform.Rotate(Vector3.down * speed * Time.deltaTime);
     }
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

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Llama_w_2Ls · Aug 20, 2020 at 06:28 PM

First of all, youre using a lot of unnecessary ifs. You can just use` Input.GetAxis("Horizontal")` to get left arrow and right arrow, and same for vertical axis. Nevertheless, using the free joystick pack in the unity asset store, you can access a class containing the types of joysticks available. Im using the variableJoystick class in this case but you can use any of the different types of joysticks. So you can basically write:

     public VariableJoystick moveJoystick;
     public VariableJoystick lookJoystick;
     //Two joystick references, one for movement and one for looking around
 
     public Rigidbody rb;
     public float moveForce = 5f;
     public float speed = 5f;
 
     private void Update()
     {
         float x = moveJoystick.Horizontal; //Equals the joystick handle's position from the center of the joystick on the horizontal axis
         float z = moveJoystick.Vertical; //Equals the joystick handle's position from the center of the joystick on the vertical axis
 
         float y = lookJoystick.Vertical;
 
         Vector3 moveDirection = transform.right * x + transform.forward * z; //Direction of movement
         Vector3 lookDirection = new Vector3(0, y, 0); //Direction of look rotation
 
         rb.AddForce(moveDirection * Time.deltaTime, ForceMode.VelocityChange); //Constantly adds force depending on where the handle of the joystick is 
         transform.Rotate(lookDirection * speed * Time.deltaTime); //Looks in the right direction depending on the other joystick's y position
     }

This uses two VariableJoystick's. One for movement and the other for the look controls. These classes are only available from the free joystick asset pack on the unity asset store. Hope it helps @Amberlewis012

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
0

Answer by Amberlewis012 · Aug 21, 2020 at 06:35 AM

Ok so I tried putting the code in the player script, however it doesn't allow me to put the joystick in the move joystick slot thing, and if I put in the joystick from the prefab it allows me to do it but doesn't do anything when I play. What did I do wrong or is it because my player is not in the actual scene or something? Thanks a lot for helping anyways @Llama_w_2Ls (also just realised this isn't a reply but whatever it works)

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 Llama_w_2Ls · Aug 21, 2020 at 09:53 AM 0
Share

You need to create a canvas first, and then drag in the VariableJoystick prefab into the canvas in your scene. Then connect that to your moveJoystick variable in the script and it should be working. Also make sure that your player has a rigidbody and that it is assigned to the public rigidbody slot in the script.

avatar image Llama_w_2Ls · Aug 21, 2020 at 09:54 AM 0
Share

Are you getting any distinct errors or warnings? If so, they might be in the script or they'll let you know what you have to do to get this working. @Amberlewis012

avatar image Amberlewis012 Llama_w_2Ls · Aug 21, 2020 at 10:29 AM 0
Share

no errors or warnings or anything, still doesn't let me do it. Normally when you drag it in the slot in the script you get a little plus icon before you release the mouse, but it just doesn't show up for me.


I think it might be because the player isn't in the hierarchy and that's part of the game mechanic so I don't think I can change it (edit: nope, doesn't work if I put the player in the scene, nor does it work with other joysticks as well), but what do I know, I'm the one asking for help here. @Llama_w_2Ls

avatar image Favour001 Amberlewis012 · Aug 21, 2020 at 10:52 AM 0
Share

Create a canvas and attached the script there

Show more comments
avatar image
0

Answer by kms171wb · Jun 03 at 05:33 AM

can you tell me where i can place this public VariableJoystick moveJoystick; public VariableJoystick lookJoystick; //Two joystick references, one for movement and one for looking around

  public Rigidbody rb;
  public float moveForce = 5f;
  public float speed = 5f;
 
  private void Update()
  {
      float x = moveJoystick.Horizontal; //Equals the joystick handle's position from the center of the joystick on the horizontal axis
      float z = moveJoystick.Vertical; //Equals the joystick handle's position from the center of the joystick on the vertical axis
 
      float y = lookJoystick.Vertical;
 
      Vector3 moveDirection = transform.right * x + transform.forward * z; //Direction of movement
      Vector3 lookDirection = new Vector3(0, y, 0); //Direction of look rotation
 
      rb.AddForce(moveDirection * Time.deltaTime, ForceMode.VelocityChange); //Constantly adds force depending on where the handle of the joystick is 
      transform.Rotate(lookDirection * speed * Time.deltaTime); //Looks in the right direction depending on the other joystick's y position
  }



player or canvas???????????

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

205 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

Related Questions

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

How to make camera position relative to a specific target. 1 Answer

Joystick for WASD & Joystick for Camera? 0 Answers

Controller Joystick Hold Delay Logic? 1 Answer

How to get 8 direction Movement with controller sticks | Topdown 3D 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