Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by Gamedope · Oct 20, 2018 at 11:48 AM · 2d game2d-platformerbackground2d spritesparallax

How can I add parallax to background sprites which are being instantiated only in play mode.

I've looked for this everywhere but none suits to my situation here:

So my 2d endless game has 2 layers of mountains in the background which I want to add parallax on, the Near layer needs to be slower than actor/camera, & the Far to be slowest. The problem is, I can't directly add script of movement to them as they are instantiated in play mode of random color selection according to the random theme colors, so they are being created one after other from the script below, but I want to add a movement on them on x axis slower than camera speed, while also letting it recreate continuously at the end of last one.

Here's the script creating new mountains :

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;

 public class PathManager : MonoBehaviour {

 public static PathManager Instance;
 public GameObject CoinFragments;
 public GameObject SlideArrow;

 public float parallaxSpeed = 5f; //

 private GameObject lastPathObject;
 private float PathXPosition;

 private GameObject lastMountainFar;
 private float MountainFarXPosition;
 private GameObject lastMountainNear;
 private float MountainNearXPosition;

 private float StarXPostion;

 private float lastCameraX;  //

 // Use this for initialization
 void Start () {
     Instance = this;
 }

 // Update is called once per frame
 void Update()
 {
                                         
 }


 private void LateUpdate()
 {
     // Check for new Near Mountain
     if (lastMountainNear != null && GamePlayCameraManager.Instance.MainCamera.transform.position.x > lastMountainNear.transform.position.x)
     {
         this.GenerateNearMountain();
     }

     // Check for new Far Mountain
     if (lastMountainFar != null && GamePlayCameraManager.Instance.MainCamera.transform.position.x > lastMountainFar.transform.position.x)
     {
         this.GenerateFarMountain();
     }
 }



 // Start Creating Mountains
 public void StartMountainCreation(){
     MountainNearXPosition = GamePlayCameraManager.Instance.MainCamera.transform.position.x;
     MountainFarXPosition = GamePlayCameraManager.Instance.MainCamera.transform.position.x;
     this.GenerateNearMountain();
     this.GenerateFarMountain();
 }

 private void GenerateNearMountain(){
     Vector3 MountainPosition = new Vector3(MountainNearXPosition - 4f, -3.6f, 10f);
     lastMountainNear = Instantiate(ThemeManager.Instance.SelectedMountainNear, MountainPosition, Quaternion.identity);
     MountainNearXPosition = MountainNearXPosition + lastMountainNear.GetComponent<Renderer>().bounds.size.x - 0.01f;
  
     //float deltaX = GamePlayCameraManager.Instance.MainCamera.transform.position.x - lastCameraX;
     //lastCameraX = GamePlayCameraManager.Instance.MainCamera.transform.position.x;
     //lastMountainNear.transform.position += Vector3.right * (deltaX * parallaxSpeed);
 }

 private void GenerateFarMountain(){
     Vector3 MountainPosition = new Vector3(MountainFarXPosition - 4f, -3.6f, 22f);
     lastMountainFar = Instantiate(ThemeManager.Instance.SelectedMountainFar, MountainPosition, Quaternion.identity);
     MountainFarXPosition = MountainFarXPosition + lastMountainFar.GetComponent<Renderer>().bounds.size.x - 0.01f;
 }
    

Here's my camera movement script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;

 public class GamePlayCameraManager : MonoBehaviour {

 public static GamePlayCameraManager Instance;   // Singleton Instance
 public Camera MainCamera;   // Main Camera

 private Vector3 offset;

 // Use this for initialization
 void Start () {
     Instance = this;
     MainCamera.transform.position = new Vector3(0, 0, -10);
 }
 
 // Update is called once per frame
 void Update () {
     
 }

 void LateUpdate()
 {
     if (GameStateManager.GameState == GameState.Set || GameStateManager.GameState == GameState.Playing)
         MainCamera.transform.position = new Vector3(this.offset.x + ActorManager.Instance.Actor.transform.position.x + 0.8f, 0, -10);
 }

 // Fixed Update Method
 void FixedUpdate(){

 }

 public void FindCameraOffset(){
     this.offset = ActorManager.Instance.Actor.transform.position - MainCamera.transform.position + new Vector3(1.5f, 0f, 0f);
 }
 }
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 JungoLeo · Oct 20, 2018 at 12:22 PM

If i understand you correct, all you need to do is to use Prefabs. Create a prefab for each mountain and add the script to it.

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.

Follow this Question

Answers Answers and Comments

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

Related Questions

"Create New Palette" doesn't actually create a palette at all 1 Answer

How do I stop my sprite from jumping in the air? 1 Answer

Exchange player position to only 3 possible places with a platform 1 Answer

Can anybody help me with this? Character bodying is falling apart. 0 Answers

Help Me! I want to make a pixel fairy! 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