- Home /
How To Make Game Remember User Input Then Play It Out, But Backwards
Ok so i made a game where once a person has made enough arrow key presses (still have to code that part) the should remember which keys were pressed and play them out when a certain condition is filled. However the keys should be played backwards. Ex. If I press up arrow down arrow right arrow; the game should play right arrow, down arrow, up arrow.
Answer by ILLUSION2006 · Aug 05, 2020 at 06:09 PM
Okay everyone I solved my problem, all I did was incorporated a timer and then at certain times started and ended the rewinds. Instead of disabling the movement, I figured out the rewind script was actually not allowing movement while playing, the only reason it looked like that, is because it would still allow the character to flip which way they were facing. To fix this I simply made a mother sprite(which btw looks the same even if it is mirrored) and just used it whenever I needed to rewind.
Answer by dorusoftware · Aug 02, 2020 at 09:49 PM
this shows the behavior you describe
Hey everyone so I watched a brackeys tutorial and its extatly what I wanted...... my only problem is that instead of the rewind only happening when the player presses enter, I need it to happen after... like 20 steps. BTW the game needs to do that automatically - Here are the 2 scripts that I need to modify.
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class CommandHandler : $$anonymous$$onoBehaviour {
public bool isRewinding = false;
List<PointInTime> pointsInTime;
Rigidbody2D rb;
// Start is called before the first frame update
void Start()
{
pointsInTime = new List<PointInTime>();
rb = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Return))
StartRewind();
if (Input.GetKeyUp(KeyCode.Return))
StopRewind();
}
private void FixedUpdate()
{
if (isRewinding)
Rewind();
else
Record();
}
void Rewind()
{
if(pointsInTime.Count > 0)
{
PointInTime pointInTime = pointsInTime[0];
transform.position = pointInTime.position;
transform.rotation = pointInTime.rotation;
pointsInTime.RemoveAt(0);
}
else
{
StopRewind();
}
}
void Record()
{
pointsInTime.Insert(0, new PointInTime(transform.position, transform.rotation));
}
public void StartRewind()
{
isRewinding = true;
rb.isKinematic = true;
}
public void StopRewind()
{
isRewinding = false;
rb.isKinematic = false;
}
}
THIS IS THE OTHER ONE -
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PointInTime { public Vector3 position; public Quaternion rotation;
public PointInTime (Vector3 _position, Quaternion _rotation)
{
position = _position;
rotation = _rotation;
}
}
While I am pretty sure that only the first one needs to be edited, I posted the second incase it helps.
Answer by PlayCreatively · Aug 02, 2020 at 02:25 PM
const int maxStepCount = 5;
Vector2[] stepRecord = new Vector2[maxStepCount];
int stepCount;
void AddStep(Vector2 dir)
{
stepRecord[stepCount++] = dir;
}
private void Update()
{
if (keyPressed)
AddStep(inputDir);
if(stepCount >= maxStepCount-1)
{
for (int i = stepRecord.Length - 1; i >= 0; i--)
This is the sequence in reverse => stepRecord[i];
}
You can work from here. Hope it helps.
Hi Im really new to this so could u explain what is going on here? I will attach my movement script if that helps (btw it is blackthornprods platformer movement script)
using UnityEngine;
public class $$anonymous$$ovement : $$anonymous$$onoBehaviour { public float speed; public float jumpForce; private float moveInput;
private Rigidbody2D rb;
private bool facingRight = true;
private bool isGrounded;
public Transform groundCheck;
public float checkRadius;
public Layer$$anonymous$$ask WhatIsGround;
private int extraJumps;
public int extraJumpsValue;
void Start()
{
extraJumps = extraJumpsValue;
rb = GetComponent<Rigidbody2D>();
}
void FixedUpdate() // We did "FixedUpdate" because we are dealing wth physics
{
isGrounded = Physics2D.OverlapCircle(groundCheck.position, checkRadius, WhatIsGround);
moveInput = Input.GetAxis("Horizontal");
Debug.Log(moveInput);
rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
if (facingRight == false && moveInput > 0)
{
Flip();
}
else if (facingRight == true && moveInput < 0)
{
Flip();
}
}
void Update()
{
if (isGrounded == true)
{
extraJumps = extraJumpsValue;
}
if (Input.GetKeyDown(KeyCode.UpArrow) && extraJumps > 0)
{
rb.velocity = Vector2.up * jumpForce;
extraJumps--;
}
else if (Input.GetKeyDown(KeyCode.UpArrow) && extraJumps == 0 && isGrounded == true)
{
rb.velocity = Vector2.up * jumpForce;
}
}
void Flip()
{
facingRight = !facingRight;
Vector3 Scaler = transform.localScale;
Scaler.x *= -1;
transform.localScale = Scaler;
}
}
Answer by ILLUSION2006 · Aug 04, 2020 at 09:06 PM
Hi I have got my rewind script working. Theres only one issue. In my rewind script I disable my movement script then re enable it after the rewind is done. My only issue is that after completing the rewind, my player character is able to move but for some reason not jump. Here is my movement script -
using UnityEngine;
public class Movement : MonoBehaviour { public float speed; public float jumpForce; private float moveInput;
private Rigidbody2D rb;
private bool facingRight = true;
private bool isGrounded;
public Transform groundCheck;
public float checkRadius;
public LayerMask WhatIsGround;
private int extraJumps;
public int extraJumpsValue;
void Start()
{
extraJumps = extraJumpsValue;
rb = GetComponent<Rigidbody2D>();
}
void FixedUpdate() // We did "FixedUpdate" because we are dealing wth physics
{
isGrounded = Physics2D.OverlapCircle(groundCheck.position, checkRadius, WhatIsGround);
moveInput = Input.GetAxis("Horizontal");
Debug.Log(moveInput);
rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
if (facingRight == false && moveInput > 0)
{
Flip();
}
else if (facingRight == true && moveInput < 0)
{
Flip();
}
}
void Update()
{
if (isGrounded == true)
{
extraJumps = extraJumpsValue;
}
if (Input.GetKeyDown(KeyCode.UpArrow) && extraJumps > 0)
{
rb.velocity = Vector2.up * jumpForce;
extraJumps--;
}
else if (Input.GetKeyDown(KeyCode.UpArrow) && extraJumps == 0 && isGrounded == true)
{
rb.velocity = Vector2.up * jumpForce;
}
}
void Flip()
{
facingRight = !facingRight;
Vector3 Scaler = transform.localScale;
Scaler.x *= -1;
transform.localScale = Scaler;
}
}
AND HERE IS MY REWIND SCRIPT #1 -
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class CommandHandler : MonoBehaviour {
public bool isRewinding = false;
List<PointInTime> pointsInTime;
Rigidbody2D rb;
//for Timer
private float secondsCount;
private int minuteCount;
// For s
// Start is called before the first frame update
void Start()
{
pointsInTime = new List<PointInTime>();
rb = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
UpdateTimerUI();// for timer
if (secondsCount > 10)
{
StartRewind();
Debug.Log("10 second rewind begins");
}
if(secondsCount > 18)
{
StopRewind();
Debug.Log("10 second rewind ended");
}
if (secondsCount > 30)
{
StartRewind();
Debug.Log("30 second rewind begins");
}
if (secondsCount > 35)
{
StopRewind();
Debug.Log("30 second rewind ended");
}
if (minuteCount > 1)
{
StartRewind();
Debug.Log("1 minute rewind begins");
}
if (minuteCount > 1.2)
{
StopRewind();
Debug.Log("1 minute rewind ends");
}
And here is rewind script #2 -
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PointInTime { public Vector3 position; public Quaternion rotation;
public PointInTime (Vector3 _position, Quaternion _rotation)
{
position = _position;
rotation = _rotation;
}
}