Search Unity

player 2d movement on Y axis

Discussion in 'Scripting' started by yariangames2016, Oct 20, 2016.

  1. yariangames2016

    yariangames2016

    Joined:
    Sep 29, 2016
    Posts:
    78
    Hi , a question please :
    player is on (-6 x , -3 y) . i need by dragging upwards or touching upwards anywhere on screen player moves to (-6 x , -2 y) . Could you please answer this question ?
     
    Last edited: Oct 23, 2016
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    What do you mean by "orientation"? Is this the "forward" direction of your object, or what?
     
  3. yariangames2016

    yariangames2016

    Joined:
    Sep 29, 2016
    Posts:
    78
    exactly . this is an endlessrunner where player is always between (-6 , -3 ) and (-6 , -2) by dragging moving .
     
  4. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Hmm, now it sounds like you're talking about position, not rotation.

    So, it sounds like you just need a script on your player with something like this in the Update event:

    Code (csharp):
    1. void Update() {
    2.     if (Input.GetMouseButton(0)) {
    3.         Vector3 pos = transform.position;
    4.         pos.x = Camera.main.ScreenToWorldPoint(Input.mousePosition).x;
    5.         transform.position = pos;
    6.     }
    7. }
     
  5. yariangames2016

    yariangames2016

    Joined:
    Sep 29, 2016
    Posts:
    78
    This script acts not like i want , with it player jumps instantly to where mouse is clicked on screen . but i need only swipe on screen my finger and player moves one unit up .
     
  6. Piflik

    Piflik

    Joined:
    Sep 11, 2011
    Posts:
    293
    I might sound like an asshole, but you probably should do some tutorials before asking on a forum. Questions like this one should all be answered by the most basic of tutorials.

    Here is a good point to start, but there is a plethora of tutorials just one google-search away.
     
  7. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Then change line 4 to:

    Code (csharp):
    1.     pos.y = pos.y + 1;
    And you should probably also change GetMouseButton to GetMouseButtonDown, so it does this only on the initial tap.

    (This is on tap; detecting a swipe will be more complicated, but as @Piflik says, some tutorials will teach you the skills you need to work it out.)
     
  8. yariangames2016

    yariangames2016

    Joined:
    Sep 29, 2016
    Posts:
    78
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerMovement : MonoBehaviour
    5. {
    6.     public float minSwipeDistY;    
    7.     private Vector2 startPos;
    8.     public GameObject player;
    9.  
    10.     void Update()
    11.     {
    12.         if (Input.touchCount > 0)
    13.         {
    14.             Touch touch = Input.touches[0];
    15.             switch (touch.phase)
    16.             {
    17.             case TouchPhase.Began:
    18.                 startPos = touch.position;
    19.                 break;
    20.             case TouchPhase.Ended:
    21.                 float swipeDistVertical = (new Vector3(0, touch.position.y, 0) - new Vector3(0, startPos.y, 0)).magnitude;
    22.                 if (swipeDistVertical > minSwipeDistY)
    23.                 {
    24.                     float swipeValue = Mathf.Sign(touch.position.y - startPos.y);
    25.                     if (swipeValue > 0)
    26.                         player.transform.position = new Vector2(-6,-2);
    27.                     else if (swipeValue < 0)
    28.                         player.transform.position = new Vector2(-6,-3);
    29.                 }
    30.                 break;
    31.             }
    32.         }
    33.     }
    34. }
    35.  
    36.  
    But i need from(-6 , -3 ) middle point , one time swipe up to -6 , -2 and one time swipe down to -6 , -4 . how can i do it?
     
  9. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    OK, that's a good start!

    I usually don't use Input.touches unless I really need multi-touch support. The first touch will work the same as mouse button 0, so by using Input.GetMouseButton and Input.mousePosition, you can test your game within the Unity editor and it'll work the same on the mobile device.

    The way you're computing swipeDistVertical would be more efficiently done as:

    Code (csharp):
    1.   float swipeDistVertical = Mathf.Abs(touch.position.y - startPos.y);
    I still don't quite understand what you want though. Are you saying there are three positions (-2, -3, and -4) that you want to switch between? Or are there just two (-2 and -3) after the initial start?

    Perhaps you could change lines 25-28 to something like this.

    Code (csharp):
    1.    float y = player.transform.position.y;
    2.    if (swipeValue > 0 && y < -2) y++;
    3.    else if (swipeValue < 0 && y > -4) y--;
    4.    player.transform.position = new Vector2(-6, y);
     
  10. yariangames2016

    yariangames2016

    Joined:
    Sep 29, 2016
    Posts:
    78
    Yes , exactly . there are three positions (-2, -3, and -4) that i want to switch between . Initial position is -3 . please guide me .
     
  11. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Then please try the code I gave you. If it doesn't do what you want, please explain exactly how it differs.
     
  12. yariangames2016

    yariangames2016

    Joined:
    Sep 29, 2016
    Posts:
    78
    the problem is that each time i need to axport apk for testing . i dont know exactly how to replace mouse functions with touch count . and also for me unity remote 5 from play store is not working , i did developer mode..and other things . can you please also tell me how exactly replace mouse clicks for touch counts ? i mean #if unity #end...something like this .
     
  13. yariangames2016

    yariangames2016

    Joined:
    Sep 29, 2016
    Posts:
    78
    By the way your new code worked out !
     
  14. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Right, this is why you should use the mouse functions.

    It's not so hard; just study the docs on Input.GetMouseButtonDown, Input.GetMouseButton, and Input.mousePosition. These are all you need. I'm not going to write the script for you, but I think you can do it — and if not, then you should work your way through some tutorials to build up your skills a bit, and then you will be able to do this (and much more!).

    Good luck,
    - Joe
     
  15. yariangames2016

    yariangames2016

    Joined:
    Sep 29, 2016
    Posts:
    78
    i replaced line 12 with Input.GetMouseButtonDown .
    but it draw error exeption in monodevelop . can you explain more please? how and with which lines must be replaced this command?thansk .
     
  16. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    It's a function, not a parameter, and the explanations are on the pages that he linked. You should definitely look at some of the tutorials on Unity Learn. Watch everything in the Scripting section, and other sections too. Also, if you can find a book or do some tutorials for C# that would be great too.
     
  17. yariangames2016

    yariangames2016

    Joined:
    Sep 29, 2016
    Posts:
    78
    Ok, i changed the code like this , but now player only moves to -6 , -4 . how to move it upwards to other desired positions ?

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerMovement : MonoBehaviour
    5. {
    6.     public float minSwipeDistY;      
    7.     private Vector2 startPos;
    8.     public GameObject player;
    9.  
    10.     void Update()
    11.     {
    12.         if (Input.GetMouseButton(0))
    13.         {
    14.            
    15.             float swipeValue = Mathf.Sign(transform.position.y - startPos.y);
    16.  
    17.             float y = player.transform.position.y;
    18.             if (swipeValue > 0 && y < -2) y++;
    19.             else if (swipeValue < 0 && y > -4) y--;
    20.             player.transform.position = new Vector2(-6, y);
    21.  
    22.          }
    23.     }
    24. }