- Home /
Trying to Highlight text with controller
Ok, huge edit:
So I've been trying to convert my main menu from MouseOver to controller (gamepad) control for selecting choices. After a night of pulling my hair out, there's been considerable progress, but haven't quite gotten over the hump yet. Essentially the menu has three separate 3d text objects, each one with a unique (but similar) script. When object 1 is color - highlighted, the variables for objects 2 and three are false. When the directional pad is pressed, script 2 (attached to object 2) changes the variable for 1 to be false, while script 1 turns 2's variable on. Those work fine, but when changing from 2 to 3 the color on three doesn't change. I'm hoping that someone might be able to see what I'm doing wrong. I tried shutting off the variable for 2 in three, like I did for 1 in two, but that just prevents 2 from turning on. Here are the three scripts. Thanks for any guidance, and God bless.
Script 1:
var isQuitButton = false;
static var text1 = true;
renderer.material.color = Color.blue;
function Update () {
if(text1 == true)
if(Text2Control.text2 == false)
if(Text3Control.text3 == false)
if(Input.GetButtonDown("Action"))
Application.LoadLevel(3);
if(Input.GetAxis("Vertical"))
ColorOff();
}
function ColorOff()
{
renderer.material.color = Color.white;
//change the color of the text
Debug.Log(" script 1 is off");
Text2Control.text2 = true;
}
Script 2
static var text2 = false;
function Update () {
if(text2 == true)
if(TextControl.text1 == false)
if(Text3Control.text3 == false)
renderer.material.color = Color.blue;
if(Input.GetButtonDown("Action"))
Application.LoadLevel(2);
if(Input.GetAxis("Vertical"))
ColorOff();
}
function ColorOff()
{
renderer.material.color = Color.white;
Debug.Log(" script 2 is off");
//change the color of the text
TextControl.text1 = false;
}
function OnButtonDown()
{
if(Input.GetButtonDown("Action"))
{
Application.LoadLevel(3);
}
}
Script 3
static var text3 = false;
function Update () {
if(text3 == true)
if(TextControl.text1 == false)
if(Text2Control.text2 == false)
renderer.material.color = Color.blue;
if(Input.GetAxis("Vertical"))
ColorOff();
}
function ColorOff()
{
renderer.material.color = Color.white;
Debug.Log(" script 3 is off");
//change the color of the text
//Text2Control.text2 = false;
}
function OnButtonDown()
{
if(Input.GetButtonDown("Action"))
{
Application.LoadLevel(3);
}
}
Answer by Visual Programmer · Sep 25, 2013 at 09:41 PM
It really depends on which controller you are targeting. Basically, you will need to set up the inputs under the input manager (Edit-->Project Settings-->Input). Then you will be able to access the inputs via the Input.GetAxis and Input.GetButton methods.
Here's the article I used to set up a xbox 360 controller. Here's another one for both xbox and playstation.
Once you have that set up the code will look something like this:
var option1:GameObject;
var option2:GameObject;
var option3:GameObject;
private var menu:Array = new Array();
private var selection:GameObject;
private var selectionNum:int = 0;
function Start(){
menu.push(option1);
menu.push(option2);
menu.push(option3);
selection = menu[selectionNum];
selection.renderer.material.color = Color.blue;
}
function Update(){
if(Input.GetAxis("Vertical"))
selection.renderer.material.color = Color.white;
//change selectionNum
//check to make sure the selection is not outside of the array
selection = menu[selectionNum];
//change color of selection
}
}
As for the menu, that's actually a lot harder for the controllers. However, what I did was use an array of options using guiText and changed the color depending on which one was selected. If you use the joysticks to control movement through the options, you will need to make sure that you lock the movement so the joysticks behave similar to GetButtonDown() otherwise you will instantly scroll to the bottom of your list.
Update Updated code above from comments
Here's a brief rundown of what you need to do to get the rest to work. Rather than giving you the rest of the code I want you to learn from this.
//changeSelectionNum -- This is how you change your selection because in the end selection = menu[selectionNum]. Therefore, if selectionNum = 1 then selection = option2
//check to make sure the selection is not outside of the array -- for this you need to make sure that selectionNum !> the length of the array and if it is to set it to the length of the array or set it back to 0. That simply depends on if you want the user to be able to get back to the top of the menu from the bottom
//change color of selection -- this is just setting the color of text in the new selection
in the end it will read like this: --reset color of current selection --make sure next selection has an option to go with it --change selection --highlight new selection
Visual, thank you so much! I'm happy to report that with your help there is progress. I don't have it figured out yet, but believe I'm heading in the right direction.
I just had a master script file which had variables for 3 different gameObjects and pushed them into an array. Then using the array I could control which one was highlighted.
Looks like this:
var option1:GameObject;
var option2:GameObject;
var option3:GameObject;
private var menu:Array = new Array();
private var selection:GameObject;
private var selectionNum:float = 0;
function Start(){
menu.push(option1);
menu.push(option2);
menu.push(option3);
selection = menu[selectionNum];
selection.renderer.material.color = Color.green;
}
function Update(){
if(/*Get Input*/){
//change color back
//change selectionNum
//check to make sure the selection is not outside of the array
selection = menu[selectionNum];
//change color of selection
}
}
I totally, incredibly appreciate you sharing that code with me, Visual, and if I cannot get this figured out, I would humbly ask if I could utilize it. That said, I want to learn how to do this. For my education, I need to learn how to do this. The code(s) I wrote are giving me fits, because I'm so close to them working, I can smell it.
hello again, Visual. I come back, suitably humbled. After working on this for a day I realize that my method, while it seems to make sense, is giving me so many headaches that I need to step away from it. Have begun implementing your code, and I see that it can work, but haven't quite wrapped my head around what I should do next. I have it written as follows:
var option1:GameObject;
var option2:GameObject;
var option3:GameObject;
private var menu:Array = new Array();
private var selection:GameObject;
private var selectionNum:float = 0;
function Start(){
menu.push(option1);
menu.push(option2);
menu.push(option3);
selection = menu[selectionNum];
selection.renderer.material.color = Color.blue;
}
function Update(){
if(Input.GetAxis("Vertical"))
selection.renderer.material.color = Color.white;
//change selectionNum
//check to make sure the selection is not outside of the array
selection = menu[selectionNum];
//change color of selection
}
I have the "Begin - Load - Exit" 3d texts assigned as the objects, and I can see that I need to do something additional with the remaining commented parts of the code. Any further assistance is of course greatly appreciated. Thank you so much for your help up to this point.
Here's a brief rundown of what you need to do to get the rest to work. Rather than giving you the rest of the code I want you to learn from this.
//changeSelectionNum -- This is how you change your selection because in the end selection = menu[selectionNum]. Therefore, if selectionNum = 1 then selection = option2
//check to make sure the selection is not outside of the array -- for this you need to make sure that selectionNum !> the length of the array and if it is to set it to the length of the array or set it back to 0. That simply depends on if you want the user to be able to get back to the top of the menu from the bottom
//change color of selection -- this is just setting the color of text in the new selection
in the end it will read like this: --reset color of current selection --make sure next selection has an option to go with it --change selection --highlight new selection