- Home /
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:
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?
If the problem is with MouseLook, is there a good way to correct this that's compatible with both desktop and mobile platforms?
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?
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.
Answer by BlueMonkMN · Aug 19, 2013 at 10:23 PM
After some thought and experimentation, I believe I have answers.
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.
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;
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.
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.