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 post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by BlueMonkMN · Aug 06, 2013 at 01:12 AM · c#charactercontrollermouselooktaptomove

First Person Controller MouseLook on Android

Background: I'm creating my first Unity project now that targeting Android is free (yay!) and would like to have the same project build for both Android and Desktop platforms. As such I have created the player as a First Person Controller prefab. And because my initial tests indicated that this prefab would allow you to look on a mobile device, but not move, I decided to add my own script for moving to the touched point, much like the Epic Citadel demo. I tried to write the script such that a mouse click on a point or a tap on the point would behave the same so I could usually test the game in the IDE without always having to deploy it on my device in order to experience the same kind of movement.

For the benefit of other Googlers of "Tap to Move" or "Touch to Move": This is the script I'm using to move to the touched point, but I doubt it has much to do with my problem:

 [RequireComponent(typeof(CharacterController), typeof(CharacterMotor))]
 public class TouchToMove : MonoBehaviour {
     
     private bool touchMoved = false;
     Vector3 moveTarget;
     bool isMovingToTarget = false;
     CharacterController controller;
     CharacterMotor motor;
     
     // Use this for initialization
     void Start () {
         controller = GetComponent<CharacterController>();
         motor = GetComponent<CharacterMotor>();
     }
     
     // Update is called once per frame
     void Update () {
         bool tapped = false;
         if (Input.touchCount > 0) {
             switch(Input.GetTouch(0).phase) {
             case TouchPhase.Began:
                 touchMoved = false;
                 break;
             case TouchPhase.Moved:
                 touchMoved = true;
                 break;
             case TouchPhase.Ended:
                 if (!touchMoved)
                     tapped = true;
                 break;
             }
         }
         if (Input.GetMouseButtonDown(0))
             tapped = true;
         if (tapped) {
             Ray r = Camera.main.ScreenPointToRay(Input.mousePosition);
             RaycastHit h;
             if (Physics.Raycast(r, out h)) {
                 moveTarget = h.point;
                 isMovingToTarget = true;
             }
         }
         
         if (isMovingToTarget) {
             Vector3 difference = moveTarget - transform.position;
             controller.SimpleMove(difference.normalized * motor.movement.maxForwardSpeed);
             if (controller.velocity.magnitude < 1)
                 isMovingToTarget = false;
         }
     }
 }

My problem: Desktop works great. But on my Nexus 7 tablet, although I can slide my finger around to look around, every time my finger first touches the screen, my camera seems to pop to a random direction, which makes the tap to move feature very unproductive. I have disabled the isMovingToTarget variable in my code above (always leaving it false) in an attempt to mostly disable the visible effects of my code, and the behavior still occurs.

My questions:

  1. Why is my camera popping to a random direction every time I start touching. Is the standard MouseLook script not designed for use on mobile devices?

  2. If the problem is with MouseLook, is there a good way to correct this that's compatible with both desktop and mobile platforms?

  3. Remembering that I want to target both mobile and desktop, am I going about this the right way? Should I be using a different prefab player?

  4. Do I need to write my own tap to move script or is there a prefab already there that I could be using for 3D tap to move functionality like that seen in Epic Citadel? I found some sample script at http://wiki.unity3d.com/index.php?title=Tap_to_Move_Drag_to_Look_iPhone but it looked overly complex. Surely all that isn't necessary just to avoid my camera popping problem.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
5
Best Answer Wiki

Answer by BlueMonkMN · Aug 19, 2013 at 10:23 PM

After some thought and experimentation, I believe I have answers.

  1. The camera is popping to a random direction because when converting a touch action into a mouse coordinate, I believe the (invisible) mouse cursor is immediately moved to the location touched (as if you had instantly moved the mouse from the last touched location to the newly touched location), which causes a large change in the mouse coordinate, which in turn results in a large value for the "Mouse X" and "Mouse Y" axis values returned by GetAxis. In this sense, the standard MouseLook script would not be ideal for use on mobile devices without some alteration, although a simple correction is possible.

  2. Adding the following code at the beginning of the Update function in the MouseLook script corrects the problem by skipping the frame where the mouse is jumping to a new location: if (Input.GetMouseButtonDown(0)) return;

  3. With the above correction (and my Tap to Move script in the question), The standard prefab seems to work quite nicely. No need to use another prefab.

  4. The simplified Tap to Move script in the question seems to suffice once MouseLook is corrected. No need for all the complications introduced by the linked iPhone script.

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 noorudheen · Aug 02, 2014 at 02:58 PM

You can use free joystick asset available aswell..

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

15 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

Related Questions

player isn't rotating along y-axis 1 Answer

how to make my character go round loops? 1 Answer

CharacterController in instance object 1 Answer

characterController rotate around an object help! 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 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