- Home /
Object Selection does not stop on next object
I've created a series of items that check their distance from each other and if one is selected and you press an arrow key it shifts the selection to the next nearest item (within 2 units using Vector.Distance) in the direction of the arrow press.
Now all four code pieces are EXACTLY the same, but when you press UP or LEFT it works fine. However pressing DOWN or RIGHT causes the selection to cut across all items until it hits the last one. As far as I've been able to tell this is due to those objects being in the + side of how Unity calculates from 0 (Top left corner).
//UPARROW works perfectly to move the selection up ONE unit
if(distance <= 2 && Input.GetKeyDown(KeyCode.UpArrow) && myPosition.y > gameState.selectedPos.y)
{
if(isSelected == false)
{
isSelected = true;
audio.PlayOneShot(selectedSound);
}
gameState.lastSelectedPos = gameState.selectedPos;
gameState.selectedPos = myPosition;
}
//DOWNARROW is exact same code, but passes through all units to the last one in the list
if(distance <= 2 && Input.GetKeyDown(KeyCode.DownArrow) && myPosition.y < gameState.selectedPos.y)
{
if(isSelected == false)
{
isSelected = true;
audio.PlayOneShot(selectedSound);
}
gameState.lastSelectedPos = gameState.selectedPos;
gameState.selectedPos = myPosition;
}
Any assistance that can be provided on this would be great as I've rewritten the malfunctioning code several times with semi-functional, but still very buggy results.
Your answer
Follow this Question
Related Questions
Click anywhere to deselect active GameObject 3 Answers
Player freezes upon attaching a "connected body" to a hinge/distance joint 0 Answers
Vertically Stationary Camera for 2D? 0 Answers
2D Animation does not start 1 Answer
C# GUI draw texture sliced 1 Answer