- Home /
How do I alter Transform.position with Time.deltaTime with faked pixelation correctly?
I am currently creating a retro, pixelated-looking game. I have it set up so that 1 linear unit = 32 pixels. That would mean that 1 pixel = 1 pixel/32 units = 0.03125 units. I, therefor am altering the characters movement like this:
character.transform.position += new Vector3(0.03125f, 0, 0);
My question is, how do I mathematically use Time.deltaTime
as a factor to get framerate independent movement with this setup? Since Time.deltaTime
is (hopefully) usually less than 1 (or simply not an integer), the scale is messed up and my character will appear to move in between the emulated "pixels." I want the movement to be, in essence, rounded or constrained to the "pixel" space of 0.03125 units at a time.
Answer by MarkFinn · Jan 07, 2013 at 03:21 AM
Note : None of this will easily work with gravity and other physics effects, so this may or may not be of much use to use.
I see two choices here. 1. Check (on every run of Update()) if enough time has passed to allow a character move by one pixel since the last move. If not, don't move the character. If so move to the nearest "pixel" distance (that is, the nearest multiple of 0.03125). You'll need to store the time of the last movement for each object you have moving this way. 2. Take the position updates out of Update, into your own co-routine, which doesn't fire every frame, but rather only every "pixel frame", which would be the same time interval as you would be checking for in the first option.
In either case you can make life much easier for yourself by storing the position of moving objects as integer X and Y values separately from the 3D space of Unity, where each int value is 1 pixel, and positioning them in play by multiplying by 0.03125 as you go.
(Part of my $$anonymous$$d is wondering if FixedUpdate() can have its frequency modified, and if that could be a much simpler solution)
Yes, certainly that would make it much easier. Thanks for the reply, I will try the co-routine method.
EDIT: Here we go. This seems to work fine. How does this code look?
void Awake()
{
StartCoroutine($$anonymous$$ove(.01f));
}
IEnumerator $$anonymous$$ove(float interval)
{
while(true)
{
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.D) || Input.Get$$anonymous$$ey($$anonymous$$eyCode.RightArrow))
{
character.transform.position += new Vector3(0.03125f, 0, 0);
yield return new WaitForSeconds(interval);
}
yield return new WaitForEndOfFrame();
}
}
The normal way of yielding a frame is "yield return null". You only need "yield return new WaitForEndOfFrame()" in relatively rare circumstances when you actually need to specifically wait for the end of a frame, since it's less efficient (as it's creating a new object every time).
Looks good to me, though as Eric noted, the WaitForEndOfFrame is overkill. "yield return null", or "yield return 0" will work just fine.
"yield return null" is still the best (equivalent to just "yield" in Unityscript); "yield return 0" performs memory allocations.
Answer by Eric5h5 · Jan 07, 2013 at 04:34 AM
I actually did something similar a while ago. The key is to use two variables, one for the "real" position that you use with Time.deltaTime as usual, then another that's rounded off to an integer that you actually use for display. As a simple example,
private var myXPos = 0.0;
function Update () {
myXPos += Time.deltaTime * 5.0;
transform.position.x = parseInt(myXPos);
}
Ok, however in my case I would, of course use the multiple of 0.03125.
Your answer
Follow this Question
Related Questions
What is the rotation equivalent of InverseTransformPoint? 3 Answers
moving objects with transform position 2 Answers
How to set euler.z to zero while keeping orientation 0 Answers
Transform.position of child object not working as intended. 1 Answer
Calc 30° left and x units forward from local forward axis 1 Answer