I’m making a pong game for iPhone and I’m stuck at one point and in need of assistance! I want to drag the Player Object just by touching on IT, not anywhere else. I can move it by clicking anywhere on the screen, but that makes it impossible to implement 2-player game. I have 2 individual player sprites and I want them both can be controlled by dragging separately on the same scene.
Hi there, I made a game today myself using this script that is similar your pong game.
using UnityEngine;
using System.Collections;
//Class to control rackets via touch
public class TouchControl : MonoBehaviour
{
//Public Variables
public GameObject player1;
public GameObject player2;
//A modifier which affects the rackets speed
public float speed;
//Fraction defined by user that will limit the touch area
public int frac;
//Private Variables
private float fracScreenWidth;
private float widthMinusFrac;
private Vector2 touchCache;
private Vector3 player1Pos;
private Vector3 player2Pos;
private bool touched = false;
private int screenHeight;
private int screenWidth;
// Use this for initialization
void Start ()
{
//Cache called function variables
screenHeight = Screen.height;
screenWidth = Screen.width;
fracScreenWidth = screenWidth / frac;
widthMinusFrac = screenWidth - fracScreenWidth;
player1Pos = player1.transform.position;
player2Pos = player2.transform.position;
}
// Update is called once per frame
void Update ()
{
//If running game in editor
#if UNITY_EDITOR
//If mouse button 0 is down
if(Input.GetMouseButton(0))
{
//Cache mouse position
Vector2 mouseCache = Input.mousePosition;
//If mouse x position is less than or equal to a fraction of the screen width
if (mouseCache.x <= fracScreenWidth)
{
player1Pos = new Vector3(-7.5f, 0.5f, Mathf.Clamp(mouseCache.y / screenHeight * speed, 0, 8));
}
//If mouse x position is greater than or equal to a fraction of the screen width
if(mouseCache.x >= widthMinusFrac)
{
player2Pos = new Vector3(7.5f, 0.5f, Mathf.Clamp(mouseCache.y / screenHeight * speed, 0, 8));
}
//Set touched to true to allow transformation
touched = true;
}
#endif
//If a touch is detected
if (Input.touchCount >= 1)
{
//For each touch
foreach (Touch touch in Input.touches)
{
//Cache touch position
touchCache = touch.position;
//If touch x position is less than or equal to a fraction of the screen width
if (touchCache.x <= fracScreenWidth)
{
player1Pos = new Vector3(-7.5f, 0.5f, Mathf.Clamp(touchCache.y / screenHeight * 8, 0, 8));
}
//If mouse x position is greater than or equal to a fraction of the screen width
if(touchCache.x >= widthMinusFrac)
{
player2Pos = new Vector3(7.5f, 0.5f, Mathf.Clamp(touchCache.y / screenHeight * 8, 0, 8));
}
}
touched = true;
}
}
//FixedUpdate is called once per fixed time step
void FixedUpdate()
{
if (touched)
{
//Transform rackets
player1.transform.position = player1Pos;
player2.transform.position = player2Pos;
touched = false;
}
}
}
I’m currently using this script so I know it works. Let me know if you use it and run into issues
There are lots of posts with drag and drop scripts. Most use Raycasting and the mouse. It is pretty straightforward to convert from mouse to touch. You will find a mouse solution here:
@Exalia
Hello … I have some objects moving in a grid. I would be glad if you could help me how I need to write code to move these objects only left, right and forward on the phone.