Search Unity

touch controls to NOT jump character

Discussion in 'Scripting' started by smokingbunny, Dec 15, 2014.

  1. smokingbunny

    smokingbunny

    Joined:
    Jul 24, 2014
    Posts:
    98
    hello folks,

    just a quick question that i have not found the answer to on my searches.

    but i have my game running on unity remote [wihch is damn helpful] and have my character. BUT it jumps to every position without dragging.
    i want to fix it so the character only moves when my finger is on the character itself rather than jumping around.

    i have the code done for movement, which is working completely fine
    Code (csharp):
    1.  
    2. if(Input.touchCount == 1){
    3.    Touchtouch = Input.GetTouch (0);
    4.    float x = -12.92f + 26 * touch.position.x / Screen.width;
    5.    float y = -9.46f + 19 * touch.position.y / Screen.height;
    6.    transform.position = newVector3(x, y, 0);
    7. }
    8.  
    ill keep searching to see if i can find something

    thanks
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    Essentially what you have to do is decide where the finger touch is in world coordinates, and compare to see if it is close enough to the character to count as "on the character."

    There are transform helpers in the Camera class (such as Camera.ScreenToWorldPoint()) or you can use the similar method of converting a screen touch into a Ray going into the scene and seeing where it intersects the plane where your character is.

    Obviously, once you decide if it is close enough, then you can know whether to do whatever jumping/not-jumping you want to do.
     
  3. smokingbunny

    smokingbunny

    Joined:
    Jul 24, 2014
    Posts:
    98
    sorry for late response did not see i had a response ;)

    thanks for that, ill have a look-see

    thanks
     
  4. smokingbunny

    smokingbunny

    Joined:
    Jul 24, 2014
    Posts:
    98
    boom. got it. it seems a bit 'meh', but i think that from the unity remote. so its a bit slow and cuts out, but does work if not going nuts ;)