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 maryamk · May 14, 2018 at 04:25 PM · transformfpsrotatelerpsmooth

smooth fps movements using joystick

I am trying to have a FPS moving inside a 3D environment using a joystick. However, with the current code that I have the movements are like steps. I want to change it so that when the user presses a button on the joystick, the player will move smoothly and continuously while the button is being pressed.

Here is what I have:

 void Update()
     {
         if (Input.GetKeyDown(KeyCode.Joystick1Button0))
          cam1.transform.Translate(0, 0, -5f);
        if (Input.GetKeyDown(KeyCode.Joystick1Button1))
             cam1.transform.Rotate(0, 5f, 0);
         if (Input.GetKeyDown(KeyCode.Joystick1Button2))
             cam1.transform.Rotate(0, -5f, 0);
         if (Input.GetKeyDown(KeyCode.Joystick1Button3))
             cam1.transform.Translate(0, 0, 5f);
 
 }

I tried using Quaternion.Lerp for rotations to the right and left, but i don't know how to exactly set the first two input parameters to the Lerp function (the from and to).

Thank you for any help :)

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 iJuan · May 14, 2018 at 04:30 PM

Use Input.GetAxis("Horizontal") for horizontal movement, and Input.GetAxis("Vertical") for vertical movement. Both methods return a float which indicates the "strength" of the movement. It's values ranges from -1 to 1

Also, you can rotate the rigidbody instead of the transform itself to make it look smoother (and keep moving a little after the stick being released, how strong it's rotation desaccelerates depends on "Angular Drag".

Comment
Add comment · Show 3 · 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 maryamk · May 14, 2018 at 05:37 PM 0
Share

Thank you iJuan

I tried it the way you said. It works nice and smoothly with keyboard buttons. However i have a problem with joystick. As soon as I press a button on joystick, the character starts moving and even when I let go of the button the movement does not stop! I think my settings in the input manager are fine, but cannot find a way to stop the character after releasing the stick.

Any ideas how to resolve it?

avatar image iJuan · May 14, 2018 at 05:43 PM 0
Share

Add "Debug.Log(Input.GetAxis("Horizontal"));" At the end of your code.

Share the resulting code, and that debug's output after releasing the stick.

avatar image maryamk iJuan · May 14, 2018 at 05:57 PM 0
Share

It's sorted! Thank you :)

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

134 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

Related Questions

Why does transform.Rotate( x, y, z ) work correctly but transform.Rotate( Vector3.forward ) does not? 1 Answer

Smooth Lerp movement? 1 Answer

Simple smooth rotation problem 4 Answers

Smoothly stop rotating 1 Answer

How to ROTATE an object without slowing the ends (lerp) 3 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