- Home /
Question by
BoydMatthews · Feb 21, 2018 at 12:44 PM ·
gamecharacter controllerrandomization
How can I randomise controls?
I'm trying to make a game where the controls are randomised each time the player dies. So one time space could be to jump but when they die space is then used to move forwards... Any idea how it can be done?
Comment
Answer by Hellium · Feb 21, 2018 at 01:03 PM
Please, next time, don't forget to try by yourself before asking for help.
private KeyCode jumpKey ;
private KeyCode leftKey ;
private KeyCode rightKey ;
private KeyCode forwardKey ;
private KeyCode backwardKey ;
private void Start()
{
RandomizeControls();
}
private void Update()
{
if( Input.GetKeyDown( jumpKey ) ) // Jump
if( Input.GetKey( leftKey ) ) // Move left
if( Input.GetKey( rightKey ) ) // Move right
// ....
}
private void RandomizeControls()
{
System.Collections.Generic.List<KeyCode> keycodes = new System.Collections.Generic.List()
{
KeyCode.Space,
KeyCode.LeftArrow,
KeyCode.RightArrow,
KeyCode.UpArrow,
KeyCode.DownArrow
} ;
int index ;
index = Random.Range( 0, keycodes.Count ) ;
jumpKey = keycodes[index] ;
keycodes.RemoveAt( index ) ;
index = Random.Range( 0, keycodes.Count ) ;
leftKey = keycodes[index] ;
keycodes.RemoveAt( index ) ;
index = Random.Range( 0, keycodes.Count ) ;
rightKey = keycodes[index] ;
keycodes.RemoveAt( index ) ;
index = Random.Range( 0, keycodes.Count ) ;
forwardKey = keycodes[index] ;
keycodes.RemoveAt( index ) ;
index = Random.Range( 0, keycodes.Count ) ;
backwardKey = keycodes[index] ;
keycodes.RemoveAt( index ) ;
}
Your answer
Follow this Question
Related Questions
getting udp package info inside unity (GlovePIE) 0 Answers
Car Tutorial error: the reference script on the Behavior is missing 0 Answers
clarify me lightmapping please 2 Answers
World hierarchy doesn't finish destroying before starting a new world? 1 Answer
assign string value from array to multiple game objects 1 Answer