Question by
rav_al · Feb 20, 2017 at 05:12 AM ·
transform.positiontouchscreentransform.translatetouchphase
Hi I'm making a game for Android(2d game). I have to move a ball using the swipe(not just vertical and horizontal). The problem is that the ball is moving in the wrong Y axis and I don't understabd why
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Ball_movement : MonoBehaviour {
private bool dirRight = false;
public float speed = 1;
Vector2 firstPos;//ball first position
Vector2 secondPressPos;//last touch of the swipe
Vector2 LastPos;//last position
public float x;//x last position
public float y;//y last position
void Swipe()
{
if (Input.touches.Length > 0)
{
Touch t = Input.GetTouch (0);
if (t.phase == TouchPhase.Began)
{
firstPos = new Vector2 (1, -5);
}
if(t.phase == TouchPhase.Ended)
{
//save ended touch 2d point
secondPressPos = new Vector2 (t.position.x, t.position.y);
if (secondPressPos.x > 1)//right
{
y = ((5-secondPressPos.x)*(-5-secondPressPos.y)+secondPressPos.y*(1-secondPressPos.y))/(1-secondPressPos.y);
x = 5;
LastPos = new Vector2 (x, y);
dirRight = true;
}
if (secondPressPos.x == 1)//centre
{
y = 9;
x = 5;
LastPos = new Vector2 (x, y);
dirRight = true;
}
if (secondPressPos.x < 1)//left
{
y = ((5-secondPressPos.x)*(-5-secondPressPos.y)+secondPressPos.y*(1-secondPressPos.y))/(1-secondPressPos.y);
x = -5;
LastPos = new Vector2 (x, y);
dirRight = true;
}
LastPos.Normalize();
print (t.position.y);
}
}
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612111507im_/https://answers.unity.com/themes/thub/images/avi.jpg)