Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Change Direction of Movement on Tap

Discussion in 'Scripting' started by androidkafkaskafkas, Jun 25, 2018.

  1. androidkafkaskafkas

    androidkafkaskafkas

    Joined:
    Jun 23, 2018
    Posts:
    16
    Hello everyone

    I am trying to change the direction of movement on tap. The character should move to down or up on tap.

    However, the code is not working properly inspite of it is okay. The character is going crazy after double clicks/taps.

    I would be appreciated if you could help me on 2D game.

    Thank you.

    Code (CSharp):
    1. public float Speed = 5f;
    2.     private bool goingRight;
    3.  
    4.     // Use this for initialization
    5.     void Start()
    6.     {
    7.  
    8.         goingRight = true;
    9.     }
    10.  
    11.     // Update is called once per frame
    12.     void FixedUpdate()
    13.     {
    14.  
    15.         if (goingRight == true)
    16.         {
    17.             transform.position += Vector3.up * Speed;
    18.         }
    19.         else
    20.         {
    21.             transform.position += Vector3.down * Speed;
    22.         }
    23.  
    24.         if (Input.GetMouseButton(0))
    25.         {
    26.             goingRight = !goingRight;
    27.         }
    28.  
    29.     }
    30. }
     
  2. NetherChef

    NetherChef

    Joined:
    Apr 2, 2018
    Posts:
    15
    Try these:
    1. Use Update() instead of FixedUpdate().
    2. Replace Input.GetMouseButton with Input.GetMouseButtonDown.
     
    androidkafkaskafkas likes this.
  3. androidkafkaskafkas

    androidkafkaskafkas

    Joined:
    Jun 23, 2018
    Posts:
    16
    Works like a charm. Thank you very much.

    But i am very new into Unity and gaming programming. Getmousebuttondown works with Android-ios or shall I switch to input.touches ? if yes, how :/ ?

    thank you once more. you reserved your seat at the heaven.

    edit: i see some kind of lagging :/
     
  4. NetherChef

    NetherChef

    Joined:
    Apr 2, 2018
    Posts:
    15
    No problem :)

    You should probably use Input.GetTouch if you're targeting touch screen devices since I think some GetMouse inputs don't work with mobile.

    What kind of lag?
     
    Lethn and androidkafkaskafkas like this.
  5. androidkafkaskafkas

    androidkafkaskafkas

    Joined:
    Jun 23, 2018
    Posts:
    16
    maybe because of my pc, i dont know. character is moving by lagging.

    would you help me to switch to mobile-get touch :/ ?
     
  6. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Do you want maybe:
    Code (csharp):
    1. transform.position += Vector3.up * Speed * Time.deltaTime;
    That would make it move 'speed units per second up'.

    Pretty sure that GetMouseButtonDown works on mobile. If you really want to confirm, make a quick build your code. :)
     
    Lethn and androidkafkaskafkas like this.
  7. androidkafkaskafkas

    androidkafkaskafkas

    Joined:
    Jun 23, 2018
    Posts:
    16
    Thank you very much for your answer methos5k.

    May I ask one more thing ?

    I have set an object to fly left to right and right to left randomly and collide to main character to end the game.

    But there are two walls around my character. The object should fly over every object. I mean bypass the walls but its stucking between those two walls and flying inside the limited empty space.

    How can I let it to fly above every object ? On hierarchy panel, its above on everything. The walls have box collide 2d pshyics.

    Thank you.
     
  8. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    If they're moving by way of the physics engine, then you can set their layer to not collide with the walls. Or if they don't need to be collision objects, you could set the flying object to a trigger collider and update any code that dealt with collision instead of trigger.

    If it's something else, you'll have to explain. :)
     
    androidkafkaskafkas likes this.