Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by ColinBurke · Jan 07, 2013 at 03:03 AM · transformmathtime.deltatime

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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.

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MarkFinn · Jan 07, 2013 at 03:22 AM 0
Share

(Part of my $$anonymous$$d is wondering if FixedUpdate() can have its frequency modified, and if that could be a much simpler solution)

avatar image ColinBurke · Jan 07, 2013 at 03:31 AM 1
Share

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();
     }
 }
avatar image Eric5h5 · Jan 07, 2013 at 04:35 AM 2
Share

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).

avatar image MarkFinn · Jan 07, 2013 at 07:09 AM 0
Share

Looks good to me, though as Eric noted, the WaitForEndOfFrame is overkill. "yield return null", or "yield return 0" will work just fine.

avatar image Eric5h5 · Jan 07, 2013 at 07:39 AM 1
Share

"yield return null" is still the best (equivalent to just "yield" in Unityscript); "yield return 0" performs memory allocations.

Show more comments
avatar image
1

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);
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ColinBurke · Jan 07, 2013 at 08:04 PM 0
Share

Ok, however in my case I would, of course use the multiple of 0.03125.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges