Code help with mobile touch drag 3D Top Down
Hiya,
I am trying to figure out where I am going wrong with this bit of code for moving the game object towards the touch.
Building a 3D, Top Down SHMUP style game for mobile. I have a script that worked for 2D top down.
I have tried to adapt it to 3D but the touch input for Y is used where as the object moves along Z axis (Portrait camera).
I have tried to figure it out as you can see below. Maybe I have set scene up wrong I don't know.
I changed the code to define individual axis of direction however the touch y position can be above 4 yet the limits on screen of movement are 4 to -4. this causes the object to simply jump straight to the top of the screen with every touch.
Thank You for any help :)
Anyway here is the code:
if (Input.touchCount > 0)
{
Debug.Log("Touched!");
Touch touch = Input.GetTouch(0);
touchPosition = Camera.main.ScreenToWorldPoint(touch.position);
Debug.Log("TouchPosX = " + touchPosition.x + " TouchPosY = " + touchPosition.y + " TouchPosZ = " + touchPosition.z);
Debug.Log("TransformPosX = " + transform.position.x + " TransformPosY = " + transform.position.y + " TransformPosZ = " + transform.position.z);
touchPosition.z = 0;
direction.x = (touchPosition.x - transform.position.x);
direction.y = (touchPosition.y - transform.position.z);
direction.z = 0;
//direction = (touchPosition - transform.position);
Debug.Log("DirectionX = " + direction.x + " DirectionY = " + direction.y + " DirectionZ = " + direction.z);
rb.velocity = new Vector3(direction.x, 0, direction.y) * moveSpeed;
if (touch.phase == TouchPhase.Ended)
{
rb.velocity = Vector3.zero;
}
}
Below is the math that takes place with the original code first then the math when I split difining the each axis value:
TouchPosX = 1.102631 TouchPosY = 10.71069 TouchPosZ = -8.948975
TransformPosX = 0 TransformPosY = 3 TransformPosZ = -4
Direction = (touchposition - transform.position);
DirectionX = 1.102631 DirectionY = 7.710693 DirectionZ = 4
--------------------------------------------------------------------
direction.x = (touchPosition.x - transofrm.position.x);
direction.y = (touchPosition.y - transform.position.z);
direction.z = 0;
TouchPosX = 0.3671412 TouchPosY = 11.41858 TouchPosZ = -8.241028
TransformPosX = 0 TransformPosY = 3 TransformPosZ = -4
DirectionX = 0.3671412 DirectionY = 15.41858 DirectionZ = 0