Heavy Rain Type Quick Time Events?

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.

bump

I would really like to know this awsell, i have been looking for a answer for some time,

Use a IEnumerator so you can yield the method

//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

Im sure this simple script would work.

function Start () {

//This action would start the quicktime event.
if (Input.GetKey(KeyCode.G)){

StartSequence();

}

}

function Update () { //This checks to see if the button is pressed or if the wrong one is.

if (Input.GetKey(KeyCode.H)){

CaseSuccess1();

}

else if{

CaseFail1();

function StartSequence(){

//This Would start the quicktime event.

function CaseSuccess1(){
//This would play the successfull animation.

}

function CaseFail1(){

//This would cancel the quicktime event.

}

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:

manager.RegisterEvent("Hero Wept", RainEvent);
manager.RegisterEvent("World destroyed", RainEvent);

At the points when it is appropriate to trigger an event (multiple places are possible; on key click …) you just:

manager.TriggerEvent("Hero Wept");

:slight_smile: Hope this helps.