- Home /
Touch control for a Pong Game
Hey Guys, can you please help me with changing keybord input script to touch control?
Here is the original script (Keyboard input):
public float paddleSpeed = 1f;
public float xLeft;
public float xRight;
private Vector3 playerPos = new Vector3(0, -9.5f, 0);
void Update () {
float xPos = transform.position.x + (Input.GetAxis("Horizontal") * paddleSpeed);
playerPos = new Vector3 (Mathf.Clamp (xPos, xLeft, xRight), -9.5f, 0f);
transform.position = playerPos;
}
For now, I have this for touch control:
public float paddleSpeed = 1f;
public float xLeft;
public float xRight;
private Ray ray;
private RaycastHit rayCastHit;
private Vector3 playerPos = new Vector3(0, -9.5f, 0);
void Update () {
if (Input.GetMouseButton (0))
{
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, out rayCastHit)){
//Missing code, not sure how to implement Mathf.Clamp and Vector3 with Ray
}
}
Are you using the 4.6 or above, the new UI on Click event system works for both mouse clicks and on touch devices.
If you just want a left and right buttons I can tell you how to do that right away with the new UI.
Let me know if you want a quick tutorial, or just click on the learn button and view the tutorials for the new UI.
Hi, thanks for your reply. Not sure if i am clear with my question. The touch control works fine. The script is attached to gameobject Cube (like paddle) and it moves on the x axes. The problem is, that I need to help with the missing part of the code. I am not able to implement the first code above to the second code with the touch.
The first script is for computers and it works. I need to implement this script with the same result to the second script. The result should be controlling the paddle on the phone, not on the keyboard.
I still think I'm not sure what you want, are you wanting to move in 3D space or just use left and right?
If just left and right you probably just need to use Input.mousePosition and give it a set value for Z then use Camera.main.ScreenToWorldPoint.
If that doesn't help can you post an image of your game so I can tell better what you're after.
Well, actualy it is exactly this tutorial: Breakout game And yes, it is 2D game as my tag says, I only need to move the paddle on the X. The first script from above is the original from the tutorial. The second script is my idea, how to change the input for mobile phones using touches. I am only not able to put the original script into the second to get the same result.
Answer by Mmmpies · Jan 24, 2015 at 05:21 PM
Missed the tag! :¬)
OK well I don't use 2D games myself but you can try this if you want to use ray casting:
public void MyRaycast()
{
myRay = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(myRay, out rayHitOut))
{
Debug.Log ("x = " + rayHitOut.point.x + " y = " + rayHitOut.point.y);
Debug.Log (Input.mousePosition);
Debug.Log (Camera.main.ScreenToWorldPoint(Input.mousePosition));
}
}
That just outputs the various Debug info, you only really need the x value. As I don't use 2D I don't know what your results are likely to be but note the x value of the raycast and the x value of the mouse outputs and which, if any match up to what you expect from that tutorial.
Then we'll set the Clamp based on your results. You might want to consider the speed of the platform as well, with your method the paddle can go from one side of the screen to the other pretty fast, you might want 2 buttons like I said in the first comment we can make them act like a controller that gains value/speed if you hold it down.
Is it really so complicated to take the first script and put him into the second with the rayCastHit? I am absolute beginner, but now the script is working. The paddle is moving according to mouse position. But still, a could not figure out, how to set the $$anonymous$$athf.Clamp to limit the movement with the padlle on the X axis. Now, I have this (the comments are the old script that was working with keyboard):
using UnityEngine;
using System.Collections;
public class Paddle : $$anonymous$$onoBehaviour {
public float paddleSpeed = 1f;
public float xL;
public float xR;
private Ray ray;
private RaycastHit rayCastHit;
private Vector3 playerPos = new Vector3(0, -9.5f, 0);
// Update is called once per frame
void Update () {
if (Input.Get$$anonymous$$ouseButton (0))
{
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, out rayCastHit)){
Vector3 position = rayCastHit.point;
position.z = 0;
position.y = -9.5f;
transform.position = position;
}
}
//float xPos = transform.position.x + (Input.GetAxis("Horizontal") * paddleSpeed);
//playerPos = new Vector3 ($$anonymous$$athf.Clamp (xPos, xL, xR), -9.5f, 0f);
//transform.position = playerPos;
}
}
Try this:
if (Input.Get$$anonymous$$ouseButton (0))
{
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, out rayCastHit)){
Vector3 position = rayCastHit.point;
float xPos = position.x;
playerPos = new Vector3($$anonymous$$athf.Clamp(xPos, xL, xR), -9.5f, 0f);
transform.position = playerPos;
}
}
Thank you for your patience. I tried your code, but it is not working. The paddle does not move at all. It even does not desplay any errors...
I just used -8 for xL and 8 for xR. If you leave them at 0 then the paddles not going to move as @fafase suggested.
Your answer
Follow this Question
Related Questions
Tap vs touch controls 1 Answer
2D Animation does not start 1 Answer
Buttons touchscreen movement bug 1 Answer
Using touch as an input for 2d game. 0 Answers