x


joystick boundary is rectangle not cycle

HI, folks! Ive got problem with distance measurement between two screen positions (Vector2) - joysticks boundary is rectangle like, but it should be like cycle (i thought using distance variable as radius and it would provide cycle like boundary).

PS. this script is based on Penelope tutorial Joystick script

                var clampedPos: Vector2;
             clampedPos.x = Mathf.Clamp(touchPos.x,guiBoundary.min.x,guiBoundary.max.x);
             clampedPos.y = Mathf.Clamp(touchPos.y,guiBoundary.min.y,guiBoundary.max.y);

             var distance: float=Vector2.Distance(defPos,clampedPos);

             if(distance<moveDistance)
             {    

              gui.pixelInset.x=clampedPos.x;
              gui.pixelInset.y=clampedPos.y;
                }

Thanks in advance!

more ▼

asked Jul 04 '11 at 08:08 PM

G_Sacristan gravatar image

G_Sacristan
137 24 28 30

That code will position the joystick within a circle of radius moveDistance, so it is not clear what problem you are having.

Jul 04 '11 at 09:49 PM Waz

The problem is that it doesnt somehow!

Jul 04 '11 at 10:43 PM G_Sacristan
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

What you need is Vector2.ClampMagnitude. If you clamp the two axis seperated it will always be a rectangle ;)

touchPos = Vector2.ClampMagnitude(touchPos,moveDistance);
more ▼

answered Jul 04 '11 at 11:55 PM

Bunny83 gravatar image

Bunny83
45.2k 11 49 207

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

Ah, given you're picture the problem is clearer - your gui boundary is smaller than you're moveDistance. You need to clip an area inside the rectangle - so the guiboundary needs to be at least moveDistance on its shortest side. Bunny83's answer is the way to code it, once you've checked that the touch is inside the rectangle (otherwise it will move the joystick for a touch anywhere, presumably not what you want).

more ▼

answered Jul 05 '11 at 05:34 AM

Waz gravatar image

Waz
6.4k 22 33 70

(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:

x214
x36
x28
x27
x2

asked: Jul 04 '11 at 08:08 PM

Seen: 1575 times

Last Updated: Jul 05 '11 at 11:46 AM