![]() ![]() Make sure you have the necessary input detection (e.g., checking for the 'R' key) and any other components or scripts your player object requires. Private IEnumerator SavePositionCoroutine()Ĭopy the entire script and attach it to your player object in Unity. ![]() It can be extended to be able to call functions in other Unity callback functions such as OnPreRender and OnPostRender. This also allows you call run a coroutine function in the main Thread. Public class PlayerRewind : MonoBehaviour This implementation allows you to call functions in the 3 most used Unity functions: Update, LateUpdate and FixedUpdate functions. If you Just want to copy and paste the script here it is using UnityEngine Remember to attach this script to your player object in Unity and adjust any additional logic or customization as needed. The RewindCoroutine is triggered when the player presses the 'R' key and gradually interpolates the player's position between the saved position and the current position over the course of 5 seconds. In this implementation, the SavePositionCoroutine runs in the background and saves the player's position every 5 seconds as long as the player is not currently rewinding ( !isRewinding). Transform.position = Vector3.Lerp(transform.position, savedPosition, t) Calculate the interpolation factor between the saved position and current position If (Input.GetKeyDown(Ke圜ode.R) & !isRewinding)Ĭreate a coroutine to rewind the player's position: private IEnumerator RewindCoroutine() Start the coroutine when your game starts or when the player becomes active: private void Start()ĭetect the input for the rewind feature and trigger the rewind process: private void Update() Here's a step-by-step guide on how you can achieve this:ĭeclare the necessary variables: private Vector3 savedPosition Ĭreate a coroutine to save the player's position every 5 seconds: private IEnumerator SavePositionCoroutine() To implement the feature you described in Unity, you can use a combination of coroutines and input detection. And your equivalent of countFrame() has to take the position and gametick as a argument. ![]() Your history is going to be a lot bigger, both in number of entries and the size of the entries so you propably need to clear old every time you add something new. This was efficient enough for the job, asuming get is called regulary. Wich si not efficient, and could easily overflow the collection if you count Frames but do not actually retreive the value. I used a simple list and cleared the when the get is called. If you removed one, the one after might be too old too.īasically it is creating a history of all counts of countFrame(). If (FrameCounter.Count > 0 & FrameCounter < decayLimit) ![]() It counts the frames, but only over the last second: using System ĭateTime decayLimit = (-1) But you also want to be able to quickly clear it after a rewind (up to before the point you rewinded too), wich requires a clear function or random access.Īs a example, I made this old and Primitive FPS counter. It has elements of a Linked List as you do not want to shift a filled collection every game tick, as a new entry is added. This historygram has similarities to a queue - in that the last added, not decayed thing is what you want out. The specific collection is up for debate. Once a point in that collecton is more then 5 seconds in the past, it can be dropped to make space for a new one. You need to save the position, every gametick, into a Collection. It also goes back "to the last manaual save", or with some sensible limit of history entries. Think of it like the Undo History of any Text Editor. In this case however, what you need is a history of positions with a maximum. The one thing fundamental to programm flow: Ticks are the one constant of game development since pong. Unity hides the ticks a bit to make it easier to programm, but they are there. Instead of seconds, you count ticks - with a known ticks/second figure. Instead of using timers, you count ticks. "Realtime" games are just games with a lot of turns, that do not wait for user input. What you want sounds like Prince of Persia: The Sands of Times rewind mechanic. InvokeRepeating("LaunchProjectile", 2, 0.I am not 100% certain for unity, but generally in games you do not use timers for games. Instance.velocity = Random.insideUnitSphere * 5 Rigidbody instance = Instantiate(projectile) InvokeRepeating method has less overhead and doesn't need the ugly while(true) construct: using UnityEngine Actually, there's a better way than using a coroutine with yield. ![]()
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |