x


iOS swipe movement

I'm doing some very simple movement. I have a character that is standing in the middle of a scene. If the user swipes to the right, the character moves to the right until he hits an obstacle.

When I test this in the Unity editor, all movement works fine. I just call Move() on the CharacterController and pass it the movement vector (which is either Vector.forward/.back/.right/.left).

But when I run it on my iOS device, it is as if the movement vector is relative to the camera location instead of the character. For example, if I call Move(Vector.forward), the character moves up into the air instead of "forward" in the scene.

Does this make sense? What am I missing? Here is some code:

    CharacterController controller = GetComponent<CharacterController>();
    float dX = Mathf.Abs(touchPosition.x - startTouchPosition.x);
    float dY = Mathf.Abs (touchPosition.y - startTouchPosition.y);
    if (dX > dY)
    {
       if (touchPosition.x > startTouchPosition.x)
         controller.Move(Vector3.right * Time.deltaTime);          
       else// if (touchPosition.x < startTouchPosition.x)
         controller.Move(Vector3.left * Time.deltaTime);
    }
    etc...
more ▼

asked Mar 26 '12 at 07:52 PM

jacksmash2012 gravatar image

jacksmash2012
312 32 43 55

Thanks, Dave A.

Basically I detect if the user is swiping to the right, left, up, or down. Then, if he is swiping to the right, I move the character in the Vector.right direction. But I'm guessing that Vector.right is w.r.t the direction of the camera, and not the character?

Mar 26 '12 at 08:45 PM jacksmash2012

Please post code.

Mar 26 '12 at 09:33 PM DaveA

I added some.

Mar 27 '12 at 12:00 AM jacksmash2012
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

Vector3.forward is world-coordinate forward. To move a character forward from the character's perspective, you probably want controller.transform.forward.

http://unity3d.com/support/documentation/ScriptReference/Transform-forward.html

more ▼

answered Mar 27 '12 at 12:36 AM

getluky gravatar image

getluky
76 1 1 3

(comments are locked)
10|3000 characters needed characters left

Probably going to need more info. How are you detecting gestures on the device? My stab-in-the-dark guess is, you are detecting x,y movement on screen, and applying that to the x,y of the character. Not x,z.

more ▼

answered Mar 26 '12 at 08:17 PM

DaveA gravatar image

DaveA
26.8k 153 171 257

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2026
x1422

asked: Mar 26 '12 at 07:52 PM

Seen: 1062 times

Last Updated: Mar 27 '12 at 12:36 AM