- Home /
Cursor Toggle in Menu (UI)
Hi guys! So this is my first time in this forum, and my first time learning Unity and programming in general. Currently I'm creating a simple UI for one of my passion projects and it consists of a Main Menu.
Here's a screenshot:
So, what I want is is for that little cursor or pointer to move up and down whenever I highlight the menu items. I would be fine if the information you give me is just the general gist and I'll just continue from there, but I'm also not opposed to specific info.
Thank you in advance guys!
Note: This may be asked already in the past but I don't know the correct keywords to use while searching so I just decided on making my own question instead.
Hello.
You should learn to use Rycast to detect what element are you clicking, then change the position of the marker
Answer by tecses1 · Mar 05, 2020 at 05:19 AM
Okay. If you want its position to be in line with New Game, Settings and Quit, then first you need to know what positions those are. Since you're using a menu here, you can either decide to hard code these or create multiple variables for each. Something like:
public Vector3 position1;
public Vector3 position2;
public Vector3 position3;
public transform myCursor;
After you know where each position is (move it in the editor maybe.) Then you need to detect if the mouse is over it. You can do this in two ways depending on your implementation.
1) If you are using 3D texts or other objects that have a collider, then using a camera ray is your move.
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit)){
if (hit.transform.name == "Button_NewGame"){
myCursor.position = position1;
}
}
2) If you are using GUI elements like GUI.Label you'll have to create a rect at the mouse position, and a rect for the text (that you use to display it.) and use something like Rect.Overlaps
void OnGUI(){
Rect newgameRect = new Rect(100,300,500,50);
GUI.Label("New Game",newgameRect);
Vector3 mousePos = Input.mousePosition;
Rect mouseRect = new Rect(mousePos.x, mousePos.y, 5,5);
if (mouseRect.Overlaps(newgameRect){
myCursor.position = position1;
}
}
Your answer
Follow this Question
Related Questions
Prefab a 2D UI? 1 Answer
Changing multiple toggle states. 0 Answers
2d - Follow cursor by having object rotate around z-axis 1 Answer
How Do I Lock the Cursor to the Center of the Screen? 1 Answer
UI Image has no sprite definition 0 Answers