Search Unity

Help with touch tutorial pls!

Discussion in 'Scripting' started by Mirdov, May 28, 2017.

  1. Mirdov

    Mirdov

    Joined:
    Apr 19, 2017
    Posts:
    5
    Hello, I'm new to Unity and in C # and I'm breaking my head to find a simple script and write one where I can use touch on android to move a rigidibody from side to side, but not with a transition, Right side of the screen when I tap the right side and appear on the left when I tap the left side.
    Example: rb is on the x = -100 axis and when you touch the right side, rb appears on the right side x = 300.
    Anyone have any tutorials or do you know a link to help me?
     
  2. Jip1912

    Jip1912

    Joined:
    Mar 13, 2015
    Posts:
    314
    You don't need a rigidbody if you want to move your gameobject instantly to a position.
    Use this:

    Code (CSharp):
    1. void OnMouseDown()
    2.     {
    3.         gameObject.transform.position = new Vector3(x,y,z)
    4.     }
    If you want to make it to go left when clicking left and right when clicking right, then just make two invisible buttons. One that covers te left side of the screen, one that covers the right side. Then you make a script with:

    Code (CSharp):
    1. public void ClickLeft()
    2. {
    3.            if (Input.GetMouseButtonDown(1))
    4.            {
    5.                    Debug.Log("Pressed left click.");
    6.                    gameObject.transform.position = new Vector3(x,y,z) // left position
    7.             }
    8.     }
    9.  
    10. public void ClickRight()
    11. {
    12.            if (Input.GetMouseButtonDown(1))
    13.            {
    14.                    Debug.Log("Pressed right click.");
    15.                    gameObject.transform.position = new Vector3(x,y,z) // right position
    16.             }
    17.     }
    Then assign this script to the button components of the buttons and assign right at the right button and left at the left button.

    Hope this helps.