- Home /
Rewind not working,Rewind not working
I'm new to unity and I'm making a snake game. I am wanting to add a feature where the snake will rewind when they die if they watch an ad. I'm having trouble with it though. when I press enter (Which starts the rewind) it just stays still. I got this from Brakeys but changed it a bit for my project. If anyone could help that would be great.
Here's my code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TimeManager : MonoBehaviour
{
public bool isRewinding = false;
List<Vector2> positions;
// Start is called before the first frame update
void Start()
{
positions = new List<Vector2>();
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Return))
{
StartRewind();
}
if (Input.GetKeyUp(KeyCode.Return))
{
StopRewind();
}
}
void FixedUpdate()
{
if (isRewinding)
{
Rewind();
}
else
{
Record();
}
}
void Rewind()
{
if (positions.Count > 0)
{
transform.position = positions[0];
positions.RemoveAt(0);
}
else
{
StopRewind();
}
}
void Record()
{
positions.Insert(0, transform.position);
}
public void StartRewind()
{
isRewinding = true;
}
public void StopRewind()
{
isRewinding = false;
}
}
Thanks!,I'm very new to unity and I'm making a snake game where if the player dies then they can watch an ad to rewind time. I followed Brakys tutorial on it and I made some changes but it doesn't seem to be working. When I try and rewind it just stays there.
Here is my code.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TimeManager : MonoBehaviour
{
public bool isRewinding = false;
List<Vector2> positions;
// Start is called before the first frame update
void Start()
{
positions = new List<Vector2>();
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Return))
{
StartRewind();
}
if (Input.GetKeyUp(KeyCode.Return))
{
StopRewind();
}
}
void FixedUpdate()
{
if (isRewinding)
{
Rewind();
}
else
{
Record();
}
}
void Rewind()
{
if (positions.Count > 0)
{
transform.position = positions[0];
positions.RemoveAt(0);
}
else
{
StopRewind();
}
}
void Record()
{
positions.Insert(0, transform.position);
}
public void StartRewind()
{
isRewinding = true;
}
public void StopRewind()
{
isRewinding = false;
}
}
Thanks!
Your answer
Follow this Question
Related Questions
Does anybody know how to fix (repositories.cfg could not be loaded) problem? 3 Answers
Change image in UI depending on the value of the slider 0 Answers
Code in void not executing 0 Answers
No overload for method 'AddForce' takes 3 arguments 1 Answer
cannot implicitly convert type unityengine.vector3' to unityengine.transform' 1 Answer