#Trying to add a combo input for my 2d game
1 messages · Page 1 of 1 (latest)
here
#archived-code-general message
ok I understand this
why is it telling me that it isnt finding "InputCombo" tho?
It should be the name of a script in your Project
make sure the asset name and the class name match
you wrote playercontrols and not PlayerControls
Because the 2nd one is the name
the first 1 is the class
so the first one should be PlayerControls and the 2nd one doesnt matter no?
public List<playercontrols.InputCombo>
ok I have no clue why that fixed it but it did
now how do I modify it to work for me?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ComboChecker : MonoBehaviour
{
public PlayerControls playerinput;
public List<PlayerControls.InputCombo> Combos;
private List<KeyCode> currentKeyCodes = new();
public void Update()
{
int currentIndex = currentKeyCodes.Count;
foreach (var inputCombo in Combos)
{
KeyCode keyCode = inputCombo.keyCodes[currentIndex];
if (Input.GetKeyDown(keyCode))
{
currentKeyCodes.Add(keyCode);
return;
}
}
}
}
Well for starters you have to check if any combo is complete. Something like this:
public bool IsComplete(InputCombo combo)
{
if (combo.keyCodes.Count != currentKeyCodes.Count)
{
return false;
}
for (int i = 0; i < currentKeyCodes.Count; i++)
{
if (currentKeyCodes[i] != combo.keyCodes[i])
{
return false;
}
}
return true;
}```
im thinking like
private void Serving()
{
if (Input.GetKeyDown(keyCode))
{
InputCombo.Add(keyCode)
}
}
yes, I understand this, but first I have to get the player input into the list no?
nah we are already keeping track of the player inputs.
oh
currentKeyCodes.Add(keyCode) adds the players input
ah makes sense
currentKeyCodes is every input the player has made
so now I add this script to my player game object
was this part supposed to be another seperate script?
public class InputCombo
{
public List<KeyCode> keyCodes;
public Vector2 hitDirection;
}
I put it in PlayerControls, but the game doesnt seem to find it
Ok no errors for now at least
Im gonna see try if I can debug.log the inputs
if it actually works
did you assign keyCodes in the inspector?
expand it
"List is empty", but thats not what I need right now, I tested it and not even anything is being printed when I pressed a button
so its not seeing my keycodes
ye because it checks if the input matches any combo, but if you don't have any there won't be any matches
you should be able to add an item to the list. Make sure your InputCombo class does not inherit from MonoBehaviour
its not MonoBehaviour
wait
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ComboList : MonoBehaviour
{
[System.Serializable]
public class InputCombo
{
public List<KeyCode> keyCodes;
public Vector2 hitDirection;
}
}
not sure where ComboList is coming from, you don't really need that
you can have the InputCombo class by itself
you assigned a combo in the Inspector?
not yet because this is what Im testing:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ComboChecker : MonoBehaviour
{
public InputCombo inputCombo;
public List<InputCombo> Combos;
private List<KeyCode> currentKeyCodes = new();
public void Update()
{
int currentIndex = currentKeyCodes.Count;
foreach (var inputCombo in Combos)
{
KeyCode keyCode = inputCombo.keyCodes[currentIndex];
if (Input.GetKeyDown(keyCode))
{
currentKeyCodes.Add(keyCode);
UnityEngine.Debug.Log("Added" + keyCode); //Here
return;
}
}
}
public bool IsComplete(InputCombo combo)
{
if (combo.keyCodes.Count != currentKeyCodes.Count)
{
return false;
}
for (int i = 0; i < currentKeyCodes.Count; i++)
{
if (currentKeyCodes[i] != combo.keyCodes[i])
{
return false;
}
}
return true;
}
}
If I press any button, its supposed to write "Added" and then the button no?
yes, assuming Combos is not an empty list
oh
go back to here and make sure you have some elements in there with hotkeys
it happens before you press any button?
no after
Because we don't reset the input when we complete a combo.
After adding a key, call
if (IsComplete(inputCombo))
{
currentKeyCodes.Clear();
//ballRB.AddForce(inputCombo.hitDirection);
}```
you also dont need the first line, public InputCombo inputCombo;
ye, all you need should be the rigidbody and then uncomment the line
that value will be the force applied to the ball, in this case 0
where do I add the force?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ComboChecker : MonoBehaviour
{
public List<InputCombo> Combos;
private List<KeyCode> currentKeyCodes = new();
private Rigidbody2D ballrb;
public GameObject Ball;
private void Awake()
{
ballrb = Ball.GetComponent<Rigidbody2D>();
}
public void Update()
{
int currentIndex = currentKeyCodes.Count;
foreach (var inputCombo in Combos)
{
KeyCode keyCode = inputCombo.keyCodes[currentIndex];
if (Input.GetKeyDown(keyCode))
{
currentKeyCodes.Add(keyCode);
if (IsComplete(inputCombo))
{
currentKeyCodes.Clear();
}
return;
}
}
}
public bool IsComplete(InputCombo combo)
{
if (combo.keyCodes.Count != currentKeyCodes.Count)
{
return false;
}
for (int i = 0; i < currentKeyCodes.Count; i++)
{
if (currentKeyCodes[i] != combo.keyCodes[i])
{
return false;
}
}
return true;
}
}