- Home /
How can I convert a OnMouseDown() command to a GetTouch input properly?
My game is designed to be played on mobile devices. Since a computer doesn't have touch input, I initially programmed it to use a OnMouse() function to select the game's block objects. In a block's script, a set of if statements are executed when clicked on. However, while still functional, the OnMouse() function didn't allow for blocks to be selected by simply sliding your finger, and was required to be individually tapped every time.
For this, I had switched to a Input.GetTouch/Touchphase.Moved method, but that caused a major problem as a random number of blocks would be selected on the grid rather than the one I have tapped.
here's the original OnMouseDown() function:
void OnMouseDown()
{
if (canBeSelected)
{
if (!selected && SelectedBlocks.Count <= 6)
{
if (firstSelect && gameObject.tag != "Operation" && SelectedBlocks.Count < 1)
{AddBlock();}
else if (SelectedBlocks.Count >= 1)
{
if(SelectedBlocks[SelectedBlocks.Count - 1].tag != gameObject.tag)
{
if ((int)gameObject.transform.position.x <= (int)SelectedBlocks[SelectedBlocks.Count - 1].transform.position.x + 1 && (int)gameObject.transform.position.x >= (int)SelectedBlocks[SelectedBlocks.Count - 1].transform.position.x - 1)
{
if ((int)gameObject.transform.position.y <= (int)SelectedBlocks[SelectedBlocks.Count - 1].transform.position.y + 1 && (int)gameObject.transform.position.y >= (int)SelectedBlocks[SelectedBlocks.Count - 1].transform.position.y - 1)
{AddBlock();}
}
}
}
}
else if (selected)
{
SpawnBlocks.RemoveBlock(myID, false);
audioData.PlayOneShot(removeAudio);
}
}
}
And here's the redone GetTouch version:
void Update()
{
if ((Input.touchCount > 0) && (Input.GetTouch(0).phase == TouchPhase.Moved))
{
Ray raycast = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
RaycastHit raycastHit;
if (Physics.Raycast(raycast, out raycastHit))
{
if (canBeSelected)
{
if (!selected && SelectedBlocks.Count <= 6)
{
if (firstSelect && raycastHit.collider.tag != "Operation" && SelectedBlocks.Count < 1)
{AddBlock();}
else if (SelectedBlocks.Count >= 1)
{
if(SelectedBlocks[SelectedBlocks.Count - 1].tag != raycastHit.collider.tag)
{
if ((int)gameObject.transform.position.x <= (int)SelectedBlocks[SelectedBlocks.Count - 1].transform.position.x + 1 && (int)gameObject.transform.position.x >= (int)SelectedBlocks[SelectedBlocks.Count - 1].transform.position.x - 1)
{
if ((int)gameObject.transform.position.y <= (int)SelectedBlocks[SelectedBlocks.Count - 1].transform.position.y + 1 && (int)gameObject.transform.position.y >= (int)SelectedBlocks[SelectedBlocks.Count - 1].transform.position.y - 1)
{AddBlock();}
}
}
}
}
else if (selected)
{
SpawnBlocks.RemoveBlock(myID, false);
audioData.PlayOneShot(removeAudio);
}
}
}
}
Please help.
Your answer
Follow this Question
Related Questions
Make an obect take priority over another when touched? 0 Answers
Raycasting UI elements - Without using Event Triggers 0 Answers
A touch'es state is always Began and the position doesn't change 1 Answer
How to show and hide an image by swiping 0 Answers
move object x axis with finger touch 0 Answers