Hi How would i add android touch screen controls for the paddle and keep the mouse controls we added in the course?
int myTouch;
const TouchPhase phasemoved = TouchPhase.Moved;
float multiplier = 5f; //This is the speed you wish for the paddle to move according to your dragging speed
Vector2 change;
Vector2 movePlayer;
static readonly Vector2 defaultVector = Vector2.zero;
float maxDistance = 10f // Max Distance the Paddle is allowed to move from its initial start position
Vector2 startpos;//Use this to store the initial start position of the Paddle
void dragMotion()
{
myTouch = Input.touchCount;
movePlayer = new Vector2(0,0);
if(myTouch > 0)
{
for(int i = 0;i<myTouch;i++)
{
if(i>0)
{
break;
//This is to ensure that the loop does not go through multiple touches from your fingers and only iterates once
}
touch = Input.GetTouch(i);
if(touch.phase == phasemoved) // If I clicked and dragged the screen
{
change = touch.deltaPosition * multiplier; //Get the Vector direction created from the difference between where my finger was last frame and this frame and times by the variable multiplier
movePlayer = new Vector2(change.x,0); // We only wish to obtain the movement in the X direction
change = defaultVector;
}
if(Vector2.Distance(this.transform.position+movePlayer, startpos)<=maxDistance) // If the position I am about to move paddle to does not exceed the max distance return true
{
this.transform.Translate(movePlayer*Time.deltaTime); // Im using the transform to translate the Paddle here though
}
movePlayer = defaultVector;
}
}
}
void Update()
{
dragMotion();
//You might need to tweak some other stuff to make this code work I initially used it for another 3D Game
}
oh and use an if statement to check if the devicetype is a mobile phone or handheld device
void Update()
{
if(SystemInfo.deviceType == DeviceType.Handheld)
{
dragMotion();
}
}