• 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 a1exi8 · Mar 07, 2013 at 10:28 PM · 2dgamescrollingparallax

Parallax scrolling

Hi, Im creating a 2d game where a character moves across a terrain. I decided to try out parallax scrolling where each plane is behind the other and adjusting the speed of the scrolling depending on their position relative to the camera. Closest scrolls fastest, etc. The main character (which is a bike) is stationary, but everything around it is scrolling giving the illusion that he's moving. The problem im having is that when the player lets go of the spacebar, the scrolling stops suddenly. Is there anyway I can make the scrolling speed up and slow down instead of stopping abruptly? This is my code so far:

public class parallax : MonoBehaviour {

 public float Speed;
 
 void Update () {
     if (Input.GetButton("Horizontal")){        
         float mautToMove = Speed * Time.deltaTime;
         transform.Translate(Vector3.left * mautToMove, Space.World);
     }
 }
 

}

Thanks!

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Kiloblargh · Mar 08, 2013 at 05:00 AM

A good quick and dirty way to get smooth anything is with Mathf.Lerp(currentValue,finalValue,somethinglessthan1)

 void Update () {
 if (Input.GetButton("Horizontal"))
     {
     if (speedNow < maxSpeed)
         {
         speedNow = Mathf.Lerp(speedNow, maxSpeed, 0.4);
         }
     }
 else
     {
     if (speedNow > 0)
         {
         speedNow = Mathf.Lerp(speedNow, 0, 0.2);
         }
     }
 if (speedNow != 0)
    {
    mautToMove = speedNow * Time.deltaTime;
    transform.Translate(Vector3.left * mautToMove, Space.World);
    }

There are better ways that give you more control- doing this will always jump toward the final value quickly and then slowly settle in to it in a very nponlinear way. In theory it should never reach the target (Zeno's Arrow paradox) but it eventually gets floating point imprecision close and so will in fact finish.

This looks good for my UI buttons, who knows if it will look good for your bike, but it is a starting place.

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 a1exi8 · Mar 08, 2013 at 05:58 PM 0
Share

Thanks for the reply, I tried this code but it doesn't seem to work. I've gotten quite a few errors

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

10 People are following this question.

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

Related Questions

2D Parallax Scrolling 1 Answer

Vertical Parallax Scrolling 1 Answer

parallax scrolling background 2 Answers

How to deal with 2D parallax in a building? 0 Answers

Unity 2D scrolling camera constantly 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