Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Quick time events linked to movement

Discussion in '2D' started by Xene_Programming, Nov 18, 2019.

  1. Xene_Programming

    Xene_Programming

    Joined:
    Nov 17, 2019
    Posts:
    2
    I'm trying to make a 2d game based on sport swimming. However, the way the character has to move for the purpose of the game is similar to that of a quick time event that works like this:

    It uses a pattern combination of W, A, D, ^, <, >, (representing arrow keys,) in order to move.
    If the player messes up the combination the character will slow down, and if they do it right, the character will continuously speed up to a maximum speed. Also, as the movement speeds up, the player will have to push the buttons faster.
    The problem is that I cant seem to find a way to do this -its been a few years since I last did anything with unity- and its very important to find a way to pull this off, or find something similar that works.
    This is the main gimmick behind the game as its only meant to be a simple reaction and rhythm game, with some elements to feel more competitive, like a timer for early levels and a high score system.
    I've been trying to find a way to do this for a few hours, so if anyone has any ideas id be more than happy to hear them.
    Also- if standard movement absolutely wont work, any ideas with an animation linked to a timer would also work
     
  2. Ted_Wikman

    Ted_Wikman

    Unity Technologies

    Joined:
    Oct 7, 2019
    Posts:
    883
    Hello Xene,
    Could you share with us a little bit of the methods you have been trying, but didn't work?
     
  3. Xene_Programming

    Xene_Programming

    Joined:
    Nov 17, 2019
    Posts:
    2
    Update:
    I've gotten a method to get a combination system up and running, But I still need a way to make it move properly. The code I have so far does allow you to move, but its manual, and goes in the wrong direction. what I need is for the character to move Left, increasing the speed by 1 each time the combo is completed.

    Code (CSharp):
    1.  
    2.  
    3. using UnityEngine;
    4.  
    5. using System.Collections;
    6.  
    7.  
    8.  
    9. public class New_Combo_System : MonoBehaviour
    10.  
    11. {
    12.  
    13.  
    14.  
    15.     bool ActivateTimerToReset = false;
    16.  
    17.     //The more bools, the less readibility, try to stick with the essentials.
    18.  
    19.     //If you were to press 10 buttons in a row
    20.  
    21.     //having 10 booleans to check for those would be confusing
    22.  
    23.  
    24.  
    25.     public float currentComboTimer;
    26.  
    27.     public int currentComboState = 0;
    28.  
    29.     public float speed;                //Floating point variable to store the player's movement speed.
    30.  
    31.  
    32.  
    33.     private Rigidbody2D rb2d;        //Store a reference to the Rigidbody2D component required to use 2D Physics.
    34.  
    35.     float origTimer;
    36.  
    37.  
    38.  
    39.     void Start()
    40.  
    41.     {
    42.  
    43.         // Store original timer reset duration
    44.  
    45.         origTimer = currentComboTimer;
    46.  
    47.     }
    48.  
    49.  
    50.  
    51.     // Update is called once per frame, yeah everybody knows this
    52.  
    53.  
    54.  
    55.     void Update()
    56.  
    57.     {
    58.  
    59.         NewComboSystem();
    60.  
    61.         //Initially set to false, so the method won't start
    62.  
    63.         ResetComboState(ActivateTimerToReset);
    64.  
    65.     }
    66.  
    67.  
    68.  
    69.     void ResetComboState(bool resetTimer)
    70.  
    71.     {
    72.  
    73.         if (resetTimer)
    74.  
    75.         //if the bool that you pass to the method is true
    76.  
    77.         // (aka if ActivateTimerToReset is true, then the timer start
    78.  
    79.         {
    80.  
    81.             currentComboTimer -= Time.deltaTime;
    82.  
    83.             //If the parameter bool is set to true, a timer start, when the timer
    84.  
    85.             //runs out (because you don't press fast enought Z the second time)
    86.  
    87.             //currentComboState is set again to zero, and you need to press it twice again
    88.  
    89.             if (currentComboTimer <= 0)
    90.  
    91.             {
    92.  
    93.                 currentComboState = 0;
    94.  
    95.                 ActivateTimerToReset = false;
    96.  
    97.                 currentComboTimer = origTimer;
    98.  
    99.             }
    100.  
    101.         }
    102.  
    103.     }
    104.  
    105.  
    106.  
    107.     void NewComboSystem()
    108.  
    109.     {
    110.  
    111.         if (currentComboState == 0)
    112.  
    113.  
    114.  
    115.         {
    116.  
    117.             if (Input.GetKeyDown(KeyCode.UpArrow))
    118.  
    119.             {
    120.  
    121.  
    122.  
    123.                 //No need to create a comboStateUpdate()
    124.  
    125.                 //function while you can directly
    126.  
    127.                 //increment a variable using ++ operator
    128.  
    129.                 currentComboState++;
    130.  
    131.  
    132.  
    133.                 //Okay, you pressed Z once, so now the resetcombostate Function is
    134.  
    135.                 //set to true, and the timer starts to reset the currcombostate
    136.  
    137.                 ActivateTimerToReset = true;
    138.  
    139.  
    140.  
    141.                 //Note that I'm to lazy to setup a switch statement
    142.  
    143.                 //that would be WAY more readable than 3 if's in a row
    144.  
    145.                 if (currentComboState == 1)
    146.  
    147.                 {
    148.  
    149.  
    150.  
    151.                     Debug.Log("1 hit");
    152.  
    153.                 }
    154.  
    155.             }
    156.  
    157.         }
    158.  
    159.             if (currentComboState == 1)
    160.  
    161.             {
    162.  
    163.                 if (Input.GetKeyDown(KeyCode.LeftArrow))
    164.  
    165.                 {
    166.  
    167.  
    168.  
    169.                     //No need to create a comboStateUpdate()
    170.  
    171.                     //function while you can directly
    172.  
    173.                     //increment a variable using ++ operator
    174.  
    175.                     currentComboState++;
    176.  
    177.  
    178.  
    179.                     //Okay, you pressed Z once, so now the resetcombostate Function is
    180.  
    181.                     //set to true, and the timer starts to reset the currcombostate
    182.  
    183.                     ActivateTimerToReset = true;
    184.  
    185.  
    186.  
    187.                     //Note that I'm to lazy to setup a switch statement
    188.  
    189.                     //that would be WAY more readable than 3 if's in a row
    190.  
    191.                     if (currentComboState == 2)
    192.  
    193.                     {
    194.  
    195.  
    196.  
    197.                         Debug.Log("2 hit");
    198.  
    199.                     }
    200.  
    201.                 }
    202.  
    203.             }
    204.  
    205.         if (currentComboState == 2)
    206.  
    207.         {
    208.  
    209.             if (Input.GetKeyDown(KeyCode.RightArrow))
    210.  
    211.             {
    212.  
    213.  
    214.  
    215.                 //No need to create a comboStateUpdate()
    216.  
    217.                 //function while you can directly
    218.  
    219.                 //increment a variable using ++ operator
    220.  
    221.                 currentComboState++;
    222.  
    223.  
    224.  
    225.                 //Okay, you pressed Z once, so now the resetcombostate Function is
    226.  
    227.                 //set to true, and the timer starts to reset the currcombostate
    228.  
    229.                 ActivateTimerToReset = true;
    230.  
    231.  
    232.  
    233.                 //Note that I'm to lazy to setup a switch statement
    234.  
    235.                 //that would be WAY more readable than 3 if's in a row
    236.  
    237.                 if (currentComboState == 3)
    238.  
    239.                 {
    240.  
    241.  
    242.  
    243.                     Debug.Log("WOOOOO");
    244.  
    245.                 }
    246.  
    247.             }
    248.  
    249.         }
    250.  
    251.         if (currentComboState == 3)
    252.  
    253.         {
    254.  
    255.  
    256.  
    257.             {
    258.  
    259.                 //Get and store a reference to the Rigidbody2D component so that we can access it.
    260.  
    261.                 rb2d = GetComponent<Rigidbody2D>();
    262.  
    263.  
    264.  
    265.                
    266.  
    267.             }
    268.  
    269.             {
    270.  
    271.                 //Store the current horizontal input in the float moveHorizontal.
    272.  
    273.                 float moveHorizontal = Input.GetAxis("Horizontal");
    274.  
    275.  
    276.  
    277.                 //Store the current vertical input in the float moveVertical.
    278.  
    279.                 float moveVertical = Input.GetAxis("Vertical");
    280.  
    281.  
    282.  
    283.                 //Use the two store floats to create a new Vector2 variable movement.
    284.  
    285.                 Vector2 movement = new Vector2(moveHorizontal, moveVertical);
    286.  
    287.  
    288.  
    289.                 //Call the AddForce function of our Rigidbody2D rb2d supplying movement multiplied by speed to move our player.
    290.  
    291.                 rb2d.AddForce(movement * speed);
    292.  
    293.             }
    294.  
    295.         }
    296.  
    297.         }
    298.  
    299.     }
    This is the code I have so far, the last lines (252 - 292, I believe) are just the basic movement code for a top down 2d game.
    I've been trying for hours now, but I cant seem to figure out a way to get it to just increase velocity automatically when the 3rd combo key is pressed. I don't know, maybe I'm way over complicating things, but that's why I'm turning to the Forums instead of suffering for the next few days until my deadline catches up to me. if anyone knows of a way to do what I'm trying to, that would be incredibly helpful.