x


drag game controller around screen

i'm trying to write a script that will allow me to touch an object on the screen of an iPhone and drag it around and when i release it the object stays where i left it, similar to the fruit ninja game (if anyone has played that).

i understand the controls for transforming position of an object using the keyboard but am at a loss as to how i can touch an object and keep the object centre as the touch position.

any ideas / suggestions?

i've been experimenting with this method (for mouse first) that i learned on answers earlier

function Update () {
    var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    var hit : RaycastHit;
    if (Physics.Raycast (ray, hit, 1000) && hit.collider.gameObject.CompareTag("GameController")) {
        self.position = hit.position;
  }
}
more ▼

asked Jun 16 '11 at 05:27 PM

rushtybaby gravatar image

rushtybaby
1 1 2 3

Welcome to UA! Please either edit your original question or post additional details in the comments on your post and not in the answers section.

Jun 16 '11 at 05:38 PM Chris D

Yes, please do! I have used my Administrative power and merged the two together, but I cannot delete the answer you have posted.

Jun 16 '11 at 06:39 PM fireDude67
(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

update: i forgot to post this, it might be useful. i've been experimenting with this method (for mouse first) that i learned on answers earlier

function Update () {
    var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    var hit : RaycastHit;
    if (Physics.Raycast (ray, hit, 1000) && hit.collider.gameObject.CompareTag("GameController")) {
        self.position = hit.position;
  }
}
more ▼

answered Jun 16 '11 at 05:30 PM

rushtybaby gravatar image

rushtybaby
1 1 2 3

Please delete this post. I have made this go into the above.

Jun 16 '11 at 06:40 PM fireDude67
(comments are locked)
10|3000 characters needed characters left

For touchscreen stuff, you'll be wanting to use Touch events. Essentially, you can replace all your references to mousePosition with the appropriate touch events.

It contains the same mouseDown/mouseUp kind of events as you're used to, but they're contained in TouchPhase.

more ▼

answered Jun 16 '11 at 05:42 PM

Chris D gravatar image

Chris D
2.5k 5 7 25

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

I'd advice checking out the DragRigidbody script that's included in Unity (if you don't have it in your assets folder, import it). It works perfectly and could serve as a good example for you.

more ▼

answered Jun 16 '11 at 06:50 PM

Joshua gravatar image

Joshua
6.4k 19 25 71

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

x65

asked: Jun 16 '11 at 05:27 PM

Seen: 663 times

Last Updated: Jun 16 '11 at 06:50 PM