Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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
2
Question by ReddKarma · Dec 02, 2019 at 05:31 PM · rotationmobiletouchdragtouch controls

How to rotate an object using touch controls

Sorry if this question is silly. Still a beginner :I

I made a C# script and attached it to my player (gameobject) so it can rotate around the Z axis using the A & D keyboard controls. However I want to make it so you can do the same on mobile but have it be so the player can drag touch there screens left and right to rotate the gameobject around the Z axis. Here is the current code below.

  public float rotatespeed = 10f;
   
     void Update()
      {
         
          if (Input.GetKey(KeyCode.D))
   
              transform.Rotate(Vector3.back, rotatespeed * Time.deltaTime);
   
   
          if (Input.GetKey(KeyCode.A))
   
              transform.Rotate(Vector3.back, -turnspeed * Time.deltaTime);
   
      }



Comment
Add comment · Show 2
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 developeration1 · Dec 02, 2019 at 06:04 PM 0
Share

So you want to make something like a virtual horizontal only joystick? or more like if you touch the left part of the phone it will turn left and if you touch right it would go right?

avatar image ReddKarma developeration1 · Dec 02, 2019 at 06:08 PM 1
Share

A horizontal joystick

3 Replies

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

Answer by developeration1 · Dec 02, 2019 at 07:04 PM

First of all, check the documentation for mobile device input at https://docs.unity3d.com/Manual/MobileInput.html .

Now, there are many ways you can do this, but try using cases, something like this example provided by Unity: https://docs.unity3d.com/ScriptReference/TouchPhase.Ended.html

In the Update method, check that you have at least 1 finger in the screen in the first place with Input.touchCount, then by taking the first finger that is in the touchscreen with Input.GetTouch(0) check its TouchPhase with a switch:

  • If the phase is TouchPhase.Began establish the starting position

  • If the phase is TouchPhase.Moved check how much the position has changed; if it's more than the starting position, move right, else, if is less than starting position move left.

  • Finally if the phase is TouchPhase.Exit just send a Debug.Log message that the finger is no longer in the screen (this one is optional).

After all, modifying your code, you'll get something like this:

 public float rotatespeed = 10f;
 private float _startingPosition;

   
 void Update()
 {
     if (Input.touchCount > 0)
     {
         Touch touch = Input.GetTouch(0);
         switch (touch.phase)
         {
             case TouchPhase.Began:
                 _startingPosition = touch.position.x;
                 break;
             case TouchPhase.Moved:
                 if (startingPosition > touch.position.x)
                 {
                     transform.Rotate(Vector3.back, -turnspeed * Time.deltaTime);
                 }
                 else if (startingPosition < touch.position.x)
                 {
                     transform.Rotate(Vector3.back, rotatespeed * Time.deltaTime);
                 }
                 break;
             case TouchPhase.Ended:
                 Debug.Log("Touch Phase Ended.")
                 break;
             }
         }
     }
 }


That would be the code, assuming that your's already work with keyboard input. Hope that helps!

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 Slashik · Oct 11, 2021 at 12:45 PM 0
Share

Awesome @developeration1 , I would like to add this case in switch statement:

 case TouchPhase.Stationary:
     startingPosition = touch.position.x;
     break;

avatar image
0

Answer by unknownsk · Feb 08 at 01:19 PM

 if (Input.touchCount > 0 && CanRotate)
             {
                 Touch touch = Input.GetTouch(0);
                 switch (touch.phase)
                 {
                     case TouchPhase.Began:
                         startingPosition = touch.position.x;
                         break;
                     case TouchPhase.Moved:
                         if (startingPosition > touch.position.x)
                         {
                             RobloxModel.transform.Rotate(Vector3.up, rotatespeed * Time.deltaTime);
                         }
                         else if (startingPosition < touch.position.x)
                         {
                             RobloxModel.transform.Rotate(Vector3.up, -rotatespeed * Time.deltaTime);
                         }
                         break;
                     case TouchPhase.Ended:
                         Debug.Log("Touch Phase Ended.");
                         break;
                     case TouchPhase.Stationary:
                         startingPosition = touch.position.x;
                         break;
                 }
             }
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
avatar image
0

Answer by nanditho · Mar 11 at 05:28 AM

If I want to rotate in any axis (X, Y, Z) depends on rotation of camera is it possible?

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

229 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

Related Questions

How to rotate an object around a fixed point by 1 Answer

How to make touch controls similar to Bumper.io 0 Answers

Trying to get the player to face the direction of a touch 1 Answer

Drag object relative to camera 1 Answer

Dragging UI Image by touch 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