Question by
Kishan_Mokaria · Dec 28, 2016 at 07:30 AM ·
c#arrayscripting beginnercharacter movement
saving user input for later character control
I want to save the basic 2D control for a character that the user provides before starting the time in scene. and can anyone help me by telling the way by which i can move my character according to the input in array. i've made a code but i am sure it doesn't work but it explains the point well.
using UnityEngine; using System.Collections;
public class charactercontrol : MonoBehaviour {
// Use this for initialization
void Start () {
int[] instructions = new int[25];
}
// Update is called once per frame
void Update () {
for (int i = 0; i < 5; i++)
if (Input.GetKey ("up")) {
instructions [i] = 1;
} else if (Input.GetKey ("right")) {
instructions [i] = 2;
}
else if (Input.GetKey ("down")) {
instructions [i] = 3;
}
else if (Input.GetKey ("left")) {
instructions [i] = 4;
}
}
}
Comment
Best Answer
Answer by SmomoGame · Dec 28, 2016 at 03:00 PM
I think you are on the right way.
Use a list to store the inputs, then make a switch to check each key
// Inputs list
public List<string> inputKeys = new List<string>() ;
void Update(){
if(Input.GetKeyDown("down")){
inputKeys.Add("down") ;
}
if(Input.GetKeyDown("right")){
inputKeys.Add("right") ;
}
if(Input.GetKeyDown("left")){
inputKeys.Add("left") ;
}
if(Input.GetKeyDown("up")){
inputKeys.Add("up") ;
}
// Check the list and do whatever you want for each key string
foreach(string key in inputKeys){
switch(key){
case "down" :
break ;
case "up" :
break ;
case "right" :
break ;
case "left" :
break ;
}
}
}