Multitouch drag object for an Airhockey game
Hi, I am a beginner in Unity. I have looked through a lot of answers related to this problem. But somehow the scripts does not work for me. So please help me out!
Should I use the same script to attach to the two mallets for the game? Here is a script I wrote with reference to others.
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 fracScreenHeight;
private float widthMinusFrac;
private float heightMinusFrac;
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;
fracScreenHeight = screenHeight/frac;
heightMinusFrac = screenHeight - fracScreenHeight;
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
Vector3 mouseCache = Camera.main.ScreenToWorldPoint(Input.mousePosition);
//If mouse x position is less than or equal to a fraction of the screen width
if (mouseCache.z <= fracScreenHeight)
{
player1Pos = new Vector3(mouseCache.x, 0f, mouseCache.z);
}
//If mouse x position is greater than or equal to a fraction of the screen width
if(mouseCache.z >= heightMinusFrac)
{
player2Pos = new Vector3(mouseCache.x, 0f, mouseCache.z);
}
//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
Ray ray = Camera.main.ScreenPointToRay(touch.position);
// touchCache = Camera.main.ScreenPointToRay(touch.position); RaycastHit hit; if (Physics.Raycast (ray, out hit, 100)) { //If touch x position is less than or equal to a fraction of the screen width if (touchCache.x <= fracScreenWidth) { transform.position = touch.position; // player1Pos = new Vector3(touchCache.x, touchCache.y, touchCache.y); } //If mouse x position is greater than or equal to a fraction of the screen width if(touchCache.x >= widthMinusFrac) { transform.position = touch.position; // player2Pos = new Vector3(touchCache.x, touchCache.y, touchCache.y); } }
}
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;
}
}
Is there a simple sample solution that I can refer to? Thank you so much!
Your answer
Follow this Question
Related Questions
How to drag multiple object simultaneously??? 0 Answers
Pinball Flippers Control with multitouch 0 Answers
Help with Inverse Mouse Panning (Middle mouse drag 0 Answers
EventTrigger multitouch 0 Answers