Search Unity

3D drag move by touch - Space Shooter

Discussion in 'Getting Started' started by Tchekito, May 8, 2019.

  1. Tchekito

    Tchekito

    Joined:
    Apr 28, 2019
    Posts:
    2
    Hi Everybody,

    I'm looking since 2 days already but I can't find the solution.
    I followed the Space Shooter tutorial, but the TouchPad does'nt fit me.

    I found other scripts in order to move the GameObject to my finger's position on the screen, but the gameObject is only moving correctly left-right. Unofrtunately, he is also moving to the top of the screen and stops at my top boundary.

    I couldn't find another thread about this issue yet, but if you have a link, please let me know.

    Here is the code :

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5.  
    6. public class SimpleTouchPad : MonoBehaviour
    7. {
    8.  
    9.     private Vector3 touchPosition;
    10.     public Rigidbody Player;
    11.     private Vector3 direction;
    12.     public float moveSpeed = 10f;
    13.  
    14.     private void Update()
    15.     {
    16.         if(Input.touchCount > 0)
    17.         {
    18.             Touch touch = Input.GetTouch(0);
    19.             touchPosition = Camera.main.ScreenToWorldPoint(touch.position);
    20.             touchPosition.z = 0;
    21.             direction = touchPosition - transform.position;
    22.             Player.velocity = new Vector3(direction.x, 0.0f, direction.y) * moveSpeed;
    23.  
    24.  
    25.             if (touch.phase == TouchPhase.Ended)
    26.                 Player.velocity = Vector3.zero;
    27.         }
    28.     }
    29.  
    30. }
    Your help would be much appreciated.

    Nicolas.
     
  2. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    How are you debugging? Debug.Log is your friend
     
  3. Tchekito

    Tchekito

    Joined:
    Apr 28, 2019
    Posts:
    2
    Hi Jeff,

    I was replying to my topic, as I just found the solution.

    Camera had a rotation of 90° on the X axis.
    I had to use y=0 instead of z=0 on the ScreenToWorld touch position, as the camera was a y=10.

    Here below the now code :

    Code (CSharp):
    1. public class SimpleTouchPad : MonoBehaviour
    2. {
    3.  
    4.     private Vector3 touchPosition;
    5.     public Rigidbody Player;
    6.     private Vector3 direction;
    7.     public float moveSpeed;
    8.  
    9.     private void FixedUpdate()
    10.     {
    11.         if(Input.touchCount > 0)
    12.         {
    13.             Touch touch = Input.GetTouch(0);
    14.             touchPosition = Camera.main.ScreenToWorldPoint(touch.position);
    15.             touchPosition.y = 0;
    16.             Vector3 movement = touchPosition - Player.transform.position;
    17.             Player.velocity = movement.normalized * moveSpeed;
    18.  
    19.  
    20.             if (touch.phase == TouchPhase.Ended)
    21.                 Player.velocity = Vector3.zero;
    22.         }
    23.     }
    24.  
    25. }
    Hope this will help someone else!

    Nico