• 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
0
Question by dolfiz · Jul 20, 2015 at 03:59 PM · offset

Scrolling background (via offset) VS Moving objects

Hi all,

I'm creating a simple 2D platform game. The player is on a fixed X coordinate on the left side of the screen and as he jumps it seems that he's moving right due to the fact that objects are moving towards him and background scrolls accordingly.

  • Player is not kinematic and he actually jumps only vertically

  • Objects he can jumps on are kinematic, moved setting their velocity different from zero during jumps, and equal to zero elsewhere

  • The background is actually a quad with a texture applied on it. On its Update() (or FixedUpdate()) method I change the X offset in order to emulate a movement with the same velocity as objects

The information about the player state is shared among objects via a static variable that is taken into account on their Update()/FixedUpdate() methods.

The background script Update() method:

     void Update () {
         if (GameManager.playerState == PlayerState.Jump) {
             pixelOffset += GameManager.horizzontalVelocity * Time.deltaTime;
             xOffset = (100 * pixelOffset) / texWidth;
             offset = new Vector2 (xOffset, 1);
             material.SetTextureOffset ("_MainTex", offset);
         }
     }

And for objects Update() and FixedUpdate() are:

     void Update () {
         if (GameManager.playerState == PlayerState.Jump) {
             velocity = new Vector2 (-GameManager.horizzontalVelocity, 0);
         } else {
             velocity = Vector2.zero;
         }
     }
     
     void FixedUpdate() {
         body2d.velocity = velocity;
     }

I tried to use both the Update() and the FixedUpdate() method on the background script but each way causes a different problem:

  • if I use Update() method the background moves smoothly and on an average basis coherently with objects, but at the moment of the jump and at the landing an hiccup occurs and frame-by-frame I've seen that actually background and objects starts and stop to move at different time

  • if I use FixedUpdate() method there are no problem at the moment of jump and on landing but the background movement itself is not smooth

Is there a solution to this or it's not possible to simulate via offset editing the movement trough physics of a rigidbody?

I hope to have explained myself well. Thank you to everyone has suggestions.

Dolfiz

Comment
Add comment · Show 2
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 nisovin · Jul 20, 2015 at 09:37 PM 0
Share

When you use FixedUpdate() on the background, do you also change Time.deltaTime to Time.fixedDeltaTime? If you don't, that may be the cause of the background not being smooth.

avatar image dolfiz · Jul 21, 2015 at 09:35 AM 0
Share

@nisovin: yes I did. I tried also weird solutions like calculate different offset during Update() and FixedUpdate(), applying always the average value between them (it turns out to have hiccups like using just Update())

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How to apply offset to touch position when dragging object 4 Answers

C# - Offset In-Air Camera from Current Object Position 1 Answer

How can I animate a sprite sheet in Java 1 Answer

Masking a plane object (which has an animated texture offset) 1 Answer

Relative rotation offset? 2 Answers


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