Look i’m total noob in unity scripting, could you please tell me what’s wrong with this code, i’m not able to implement the touch, i want when user taps on the screen, to store that x and y coordinate and move the object to that location, i’m pasting the code, please tell me how to implement it
void controlTouch()
{
float x,z;
Vector3 mousePos = Input.mousePosition;
mousePos = cam.ScreenToWorldPoint(new Vector3(mousePos.x, mousePos.y, cam.transform.position.y - transform.position.y));
targetRotation = Quaternion.LookRotation(mousePos - new Vector3(transform.position.x, 0, transform.position.z));
transform.eulerAngles = Vector3.up * Mathf.MoveTowardsAngle(transform.eulerAngles.y, targetRotation.eulerAngles.y, rotationSpeed * Time.deltaTime);
if(Input.touches.Length > 0)
{
//if(Input.touches[0].phase == TouchPhase.Moved)
//{
x = Input.GetTouch(0).deltaPosition.x;
z = Input.GetTouch(0).deltaPosition.y;
//x = Input.touches[0].deltaPosition.x * runSpeed * Time.deltaTime;
//z = Input.touches[0].deltaPosition.y * runSpeed * Time.deltaTime;
//}
/*else
{
x = 0.0f;
z = 0.0f;
}*/
}
else
{
x = 0.0f;
z = 0.0f;
}
Vector3 input = new Vector3(x,0,z);
currentVelocityModifer = Vector3.MoveTowards(currentVelocityModifer,input,acceleration * Time.deltaTime);
Vector3 motion = currentVelocityModifer;
motion *= (Mathf.Abs(input.x) == 1 && Mathf.Abs(input.z) == 1) ? 0.7f : 1;
motion *= (Input.GetButton("Run")) ? runSpeed : walkSpeed;
motion += Vector3.up * -8;
controller.Move(motion * Time.deltaTime);
}