Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
This question was closed Apr 20, 2014 at 09:44 PM by _1 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by _1 · Feb 24, 2014 at 09:58 PM · controllerjoystickinput.getaxis

Using Joystick for movement

Hello there, I am making a 2d xbox game and I have a code that makes the character move depending on the the left joystick axis

so here's the code

 #pragma strict
 
 private var movement : Vector3 = Vector3.zero;
 
 var jumpheight = 4;
 
 function Update () {
 
 
 
 var controller : CharacterController = GetComponent(CharacterController);
 
 var grounded = controller.isGrounded; 
 
 var LeftJoyStickPressed = false;
 
 
 
 var LeftJoystick = Input.GetAxis("360_LeftJoystick");
 
 if(LeftJoystick == false){
 
 LeftJoystick = 0;
 LeftJoyStickPressed = false;
 } 
 
 if(LeftJoystick == -1)
 {
 
 transform.position -= transform.right * Time.deltaTime;
 LeftJoyStickPressed = true;
 }
 
 if(LeftJoystick == 1)
 {
 
 transform.position += transform.right * Time.deltaTime;
 LeftJoyStickPressed = true;
 }
 
 
 if(!grounded){
 
 movement.y += Physics.gravity.y * Time.deltaTime;
 
 controller.Move (movement * Time.deltaTime);
 
 }
 
 if(Input.GetButton("360_AButton") && grounded == true){
 
 movement.y = jumpheight;
 
 controller.Move(movement * Time.deltaTime);
 
 }
 
 
 
 
 
 Debug.Log("" + grounded);
 Debug.LogWarning("" + Input.GetButtonDown("360_AButton")); 
 Debug.LogError("" + Input.GetAxis("360_LeftJoystick"));
 Debug.Log("" + LeftJoyStickPressed);
 
  }

So my problem is that when I am not tapping the left joystick the value of left joystick is 1 so the character position is always incrementing by one. I don't know how to fix this. Help would be very much appreciated, Thank you :)

Comment
Add comment · Show 3
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 Invertex · Feb 25, 2014 at 03:31 AM 1
Share

Well, firstly, you don't need the double conditionals for your player movement. Just use:

 if($$anonymous$$athf.Abs(Input.GetAxis("360_LeftJoystick")) > 0.08) //$$anonymous$$athf.Abs converts all values to a positive value, so this will tell you if it's below or above 0, by at least 0.08 (to compensate for joystick over-sensitivity)
 {
 transform.position = moveSpeed * LeftJoystick * Time.deltaTime; //add a moveSpeed Vector2 variable so you can set your move speed...
 }

I'm not sure this code is even working:

 if(LeftJoystick == false){
  
 LeftJoystick = 0;
 LeftJoyStickPressed = false;
 } 

LeftJoystick is a float, not a bool, it should be if == 0. Though you shouldn't even need this because you can just add:

 else{
     LeftJoyStickPressed = false;
 }

After the $$anonymous$$athf.Abs line I gave ya.

Also, are you not using a Rigidbody?

avatar image _1 · Feb 25, 2014 at 09:45 PM 0
Share

no rigidbody

avatar image _1 · Feb 25, 2014 at 09:51 PM 0
Share

thanks for your time and help :)

I also turned down the sensitivity of the axis big time and that helped to

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

21 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

Related Questions

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

problems with virtual joystick for IOS 1 Answer

How do I make the Joystick Analog Stick work like the Mouse Input? 0 Answers

Dual joystick Position always resets on playing 1 Answer

Third Person Controller Pauses When Turning 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