Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

]How can I move an object along the axis using touch input?[ SOLVED]

Discussion in 'Scripting' started by tomowale, Sep 24, 2016.

  1. tomowale

    tomowale

    Joined:
    Feb 14, 2015
    Posts:
    308
    Hey guys, currently I am making a game where you move a barrel to collect coins. I'm at the point of the game where I need to add in mobile input controls. I can move the player left and right using the keyboard but I am not sure how to do the same on mobile devices. I simply want my barrel to move when my player puts their finger on the barrel and moves it left and right to collect coins. I've looked up tutorials everywhere but can't seem to find any that lets you follow the game object using your finger on the x axis.This is my first game, so I'm very new to touch controls.

    I generally try to help out people who give me help by providing the script I've attempted. However, in this case I only have the keyboard controls script because I don't know how to use touch for this problem.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class BarrelControlsScript : MonoBehaviour {
    5.     public float barrelspeed = 3f;
    6.  
    7.     // Use this for initialization
    8.     void Start () {
    9.        
    10.     }
    11.  
    12.     // Update is called once per frame
    13.     void Update () {
    14.         transform.Translate(Vector3.right * Time.deltaTime * Input.GetAxis("Horizontal") * barrelspeed);
    15.     }
    16. }
    17.  
     
  2. jimroberts

    jimroberts

    Joined:
    Sep 4, 2014
    Posts:
    560
  3. IsGreen

    IsGreen

    Joined:
    Jan 17, 2014
    Posts:
    206
    OnMouseDown and OnMouseDrag work in touch system. Mesh need collider component to work correctly.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [RequireComponent(typeof(Collider))]
    5. public class TouchMove : MonoBehaviour {
    6.  
    7.     float distance = 0f;
    8.  
    9.     void OnMouseDown()
    10.     {
    11.         #if UNITY_STANDALONE
    12.         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    13.         #endif
    14.  
    15.         #if UNITY_ANDROID
    16.         Touch touch = Input.GetTouch(0);
    17.         Ray ray = Camera.main.ScreenPointToRay(touch.position);
    18.         #endif
    19.         this.distance = Vector3.Distance(ray.origin, this.transform.position);
    20.     }
    21.  
    22.     void OnMouseDrag()
    23.     {
    24.         #if UNITY_STANDALONE      
    25.         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    26.         #endif
    27.  
    28.         #if UNITY_ANDROID
    29.         Touch touch = Input.GetTouch(0);
    30.         Ray ray = Camera.main.ScreenPointToRay(touch.position);
    31.         #endif
    32.  
    33.         this.transform.position = ray.origin + ray.direction * distance;
    34.     }
    35. }
    I have not been able to verify the code in the android system.In PC working properly.
     
    Last edited: Sep 24, 2016
    ravinder_emilence likes this.
  4. tomowale

    tomowale

    Joined:
    Feb 14, 2015
    Posts:
    308
    Yes I know I do but I was just confused on how to do so.
     
  5. tomowale

    tomowale

    Joined:
    Feb 14, 2015
    Posts:
    308
    Hey IsGreen. Thank you for the answer. I can't try this right now but I will try when I get back to unity.
     
  6. tomowale

    tomowale

    Joined:
    Feb 14, 2015
    Posts:
    308
    Weird.I'm getting the error ArgumentException : Index out of bounds.
     
  7. tomowale

    tomowale

    Joined:
    Feb 14, 2015
    Posts:
    308
    Update : It works on android! But do you know how I can limit input so you can only move along the x-axis?
     
  8. tomowale

    tomowale

    Joined:
    Feb 14, 2015
    Posts:
    308
    Update: I fixed this by clamping the z and y axis! Thanks to everyone who replied to the thread!
     
  9. Manu1995

    Manu1995

    Joined:
    Jul 26, 2018
    Posts:
    1
    How you done the clamping of y and z axis?