• 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 Iggy-was-taken · May 31, 2014 at 08:38 PM · backgroundparallax

The Parallax goes way too fast

So Im trying to make an parallax backround with one background and then one foreground. I followed brackeys tutorial and have no idea of what I just wrote, Im pretty new and I understand certain parts, but not the overall connection between the codes. However, the background moves way too fast and when I change the smoothing value nothing noticable really happends so please help me :S

EDIT: the code's C#

 using UnityEngine;
 using System.Collections;
 
 public class Parallaxing : MonoBehaviour {
 
     public Transform[] backgrounds;      // List of all the back and foreground to be parallaxed
     private float[] parallaxScales;       // The proportion of the camera's movement to move the backround by
     public float smoothing = 1f;         // How smooth the parallax is going to be. Make sure to set this above 0
 
     private Transform cam;               // Reference to the main cameras transform
     private Vector3 previousCamPos;      // the position of the camera in the previous frame
 
     //Awake = Called before start
     void Awake (){
      //set up the camerarefrence
         cam = Camera.main.transform;
 
     }
     
     
     // Use this for initialization
     void Start () {
         //the previous frame had the current frames camera position
         previousCamPos = cam.position;
 
         // assingning corespongding parallaxScales
         parallaxScales = new float[backgrounds.Length];
         for (int i = 0; i < backgrounds.Length; i++){
             parallaxScales[i] = backgrounds[i].position.z*-1;
                                    }
 
     
     }
     
     // Update is called once per frame
     void Update () {
     
             // for each background
             for (int i = 0; i < backgrounds.Length; i++){
               // the parallax is the opposite of the cameramovement because the previous frame multiplied by the scale
                 float parallax = (previousCamPos.x = cam.position.x) * parallaxScales[i];
 
                 // set a target x position which is the current position plus the parallax
                 float backgroundTargetPosX = backgrounds[i].position.x + parallax;
 
                 // creaye a target position which is the background's current position with it's target x position 
                 Vector3 backgroundTargetPos = new Vector3 (backgroundTargetPosX, backgrounds[i].position.y,backgrounds[i].position.z);
 
                 // fade between current position and the target position using lerp
                 backgrounds[i].position = Vector3.Lerp (backgrounds[i].position, backgroundTargetPos, smoothing = Time.deltaTime);
             }
 
             // set the previousCamPos to the cameras position at the end of the frame
             previousCamPos = cam.position;
     }
 }
 
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
1
Best Answer

Answer by iwaldrop · Jun 01, 2014 at 04:08 AM

I think the root of your problem lies on line 41, where you assign cam.position.x to previousCamPos.x instead of subtracting the former from the latter. The same goes for line 50 where you assign smoothing to Time.deltaTime.

Personally, I'd have a data structure that mated Transforms and speeds together, like so:

 enter code hereusing UnityEngine;
 using System.Collections;
 
 public class Parallaxing : MonoBehaviour
 {
     public ParallaxObject[] objects;
     
     private Vector3 previousCamPos;      // the position of the camera in the previous frame
     private Transform cam;               // Reference to the main cameras transform
 
     void Awake()
     {
         cam = Camera.main.transform;
     }
 
     void Start()
     {
         previousCamPos = cam.position;
 
         if (objects.Length == 0)
         {
             enabled = false;
         }
         else
         {
             foreach (ParallaxObject po in objects)
                 po.SetParallaxScale();
         }
     }
 
     void Update()
     {
         Vector3 deltaMovement = previousCamPos - cam.position;
         previousCamPos = cam.position;
         foreach (ParallaxObject po in objects)
             po.Evaluate(deltaMovement);
     }
 }
 
 [System.Serializable]
 public class ParallaxObject
 {
     public Transform transform;
 
     private Vector3 parallaxScale;
     private float depth;
 
     public void SetParallaxScale(float scale = -1)
     {
         depth = transform.position.z;
         parallaxScale = (Vector3.right + Vector3.up) * depth * scale;
     }
 
     public void Evaluate(Vector3 cameraMovement)
     {
         Vector3 targetPosition = Vector3.zero;
         for (int i = 0; i <= 2; i++)
             cameraMovement[i] *= parallaxScale[i];
         targetPosition = (transform.position + cameraMovement);
         targetPosition.z = depth;
 
         // fade between current position and the target position using lerp
         transform.position = Vector3.Lerp (transform.position, targetPosition, Time.deltaTime);
     }
 }
 

This allows you to add more options to each ParallaxObject as you need it. Honestly, I refactored the code so much since I started that I'd actually think a list of transforms and a helper function that took the parallaxed transform as a parameter would be better than another class which takes the camera delta as a parameter, but hopefully this gives you some idea.

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 iwaldrop · Jun 01, 2014 at 04:09 AM 0
Share

The above code also parallaxes on the y axis as well. ;)

avatar image
0

Answer by Iggy-was-taken · Jun 06, 2014 at 07:21 PM

THANK YOU!

Comment
Add comment · 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

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

21 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Background Image as Orthographic 2D Game Background - and quick 2D parallaxing question 1 Answer

how to fix jitter on a parallax? 2 Answers

Is this the best way to do an AutoParallax Background ? 1 Answer

trouble with getting the background to show past the midground in 2d parallax backgrounds 0 Answers

Parallax effect for background [C#] just a simple math error but I can't fix it 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