Search Unity

Dragging by touching - Only X axcis

Discussion in 'Scripting' started by Tabacashi, May 23, 2015.

  1. Tabacashi

    Tabacashi

    Joined:
    Oct 3, 2014
    Posts:
    8
    Hello.
    Could anyone help me with script? I want to drag objects with concrete tag but in only X axcis. I'm working at 2D game on Adroid so i need to have touching scripts.

    Thanks all.
     
  2. Tabacashi

    Tabacashi

    Joined:
    Oct 3, 2014
    Posts:
    8
    Know anyone how to do it?
     
  3. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    http://docs.unity3d.com/ScriptReference/Input.GetTouch.html
    is the basic "touch" input method.

    You'll need to use the example at the bottom of the page to get a raycast.

    there are a variety of raycasts which take different parameters (http://docs.unity3d.com/ScriptReference/Physics.Raycast.html) you'll need one which takes a ray and has a "out hit". Use the "hit" to see if the thing you are touching over has that tag.

    Store the "thing" in some variable if it does.

    Then back to the first example on GetTouch, to move the "thing" from the variable (if there is one).

    i.e. when you first touch the screen the code finds out if you are touching a concrete tagged object, if it is it stores a reference to it and if you move your finger in a subsequent frame without having let go it will update the objects position.

    Give it a go starting with the basic "raycast when I touch the screen", then move onto the next bits, if you get stuck, post what you've managed to do and someone will help you out. :)

    basic raycast tutorial: https://unity3d.com/learn/tutorials/modules/beginner/physics/raycasting
     
    Tabacashi likes this.