Well I’m making a story based game, and I realized how much more immersive it is if you place quick time events at the right time. I just need a demo script for something like:
if (player hits object) {
//display on GUI quick time event trigger if(correct quick time event trigger){
animation.play(animation)
}
else{
//fail
}
}
Obviously this doesn’t do anything, i just need the code for if the user hits a key at a certain time, getkeydown works, but for the whole game, so that wont do much.
//Make sure player or trigger object has ridigid body..
//Attach collider of choice and check trigger boolean.
using System.Collections.Generic;
public List<SequenceOfOperations> sequenceOfOps = new List<bool>();
public void Awake()
{
AddBackStabSequence();
}
public void AddBackStabSequence()
{
sequenceOfOps.Add("Strafe", KeyCode.A);
sequenceOfOps.Add("BackStab", KeyCode.B);
}
public void OnTriggerEnter(Collision col)
{
if(col.gameObject.Getcomponent<MyCustomPlayerScript>() )
{
//The Player hit this trigger start sequence.
StartCoroutine( BeginBackStabSequence() );
}
}
public float timePerPress = 0.5f; // half a second to press the correct key.
public IEnumerator BeginBackStabSequence()
{
float startTime = Time.time;
int index = 0;
float timePassed = startTime;
while(index < sequenceOfOps.Count AND timePassed < timePerPress )
{
//Tell them to press sequenceOfOps[index].keyToPress
if(Input.GetKeyDown(sequenceOfOps[index].keyToPress))
{
sequenceOfOps[index].complete = true;
//Preform action.
//yield return new WaitForSeconds(animation["clipName"].Length // Wait for the animation to finish.
startTime = Time.time;
index++;
}
timePassed = Time.time-startTime;
yield return new WaitForSeconds(0.01f);
}
bool finishedSequence = true;
for(int i = 0; i < sequenceOfOps.Count; i++)
{
if(!sequenceOfOps[i].completed)
{
finishedSequence = false;
}
}
// finishedSequence will be true if they completed or false if they didnt...
}
[System.Serialized]
public class SequenceOfOperations
{
public SequenceOfOperations(string name, KeyCode key)
{
seqName = name;
keyToPress = key;
}
//Class so we can make a list of these operations...
public bool completed = false;
public string seqName = "MySequenceName";
public KeyCode keyToPress = KeyCode.Alpha1;
}
Typed this entire thing in Unity forums… terrible haha
I would recommend using a simplified state engine for something like this. In a nutshell you would need 2 classes to start with (this could be designed in a hundred different ways; so below is just one possible skeleton of a solution):
public class EventManager : Monobehaviour
{
private Hashmap events = null;
private string triggeredEvent = null;
void Start()
{
}
void Update()
{
if (triggeredEvent == null)
return;
Event e = events.Get(triggeredEvent);
if (!e.isStarted())
e.Start();
if (!e.Update())
triggeredEvent = null;
}
// this method will be used to register events with a manager
public void RegisterEvent(string name, Event e)
{
events.Add(name, e);
}
public void TriggerEvent(string name)
{
triggeredEvent = name;
}
}
public abstract class Event
{
protected bool started = false;
public virtual void Start()
{
started = true;
}
public abstract bool Update();
public bool isStarted()
{
return started;
}
}
Create a class that inherits the Event class above. Lets say - RainEvent. Register all possible events with EventManager. and you can have multiple events causing RainEvent. Like: