Problem with parallaxing and tiling: https://www.youtube.com/watch?v=pg5TPZNEYuQ Anyone h

Updated on December 6, 2017 in [A] Brackeys Courses
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
4 on December 3, 2017

Problem with parallaxing and tiling:

 

https://www.youtube.com/watch?v=pg5TPZNEYuQ

 

Anyone have any idea how to fix?

This has only happened after adding the BackgroundMountainsContainers.

It is because the tiling script is making thousands of clones. how do i stop this?

  • Liked by
Reply
3 on December 4, 2017

Nope

on December 5, 2017

ok, thanks anyway

Guru
on December 6, 2017

As in nope because you don’t give sufficient information

on December 6, 2017

Should’ve said:

parallaxing:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Parallaxing : MonoBehaviour {
public Transform[] backgrounds; //aray (list) of all the back and foregrounds to be parallaxed
private float[] parallaxScales; //The proportion of the camera's movement to move the backgrounds by
public float smoothing = 1f; //how smooth the parallax is going to be. set above 0
private Transform cam; //reference to main cam's transform
private Vector3 previousCamPos; //The position of the camera in the previous frame
//called bfore start(). Great fo references.
void Awake() {
//set up camera reference
cam = Camera.main.transform;
}
// Use this for initialization
void Start () {
// the prev frame
previousCamPos = cam.position;
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 (int i = 0; i < backgrounds.Length; i++) {
float parallax = (previousCamPos.x - cam.position.x) * parallaxScales[i];
float backgroundTargetPosX = backgrounds[i].position.x + parallax;
Vector3 backgroundTargetPos = new Vector3(backgroundTargetPosX, backgrounds[i].position.y, backgrounds[i].position.z);
backgrounds[i].position = Vector3.Lerp(backgrounds[i].position, backgroundTargetPos, smoothing * Time.deltaTime);
}
previousCamPos = cam.position;
}
}

Tiling:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent (typeof(SpriteRenderer))]
public class Tiling : MonoBehaviour {
public int offsetX = 2;
public bool hasARightBuddy = false;
 public bool hasALeftBuddy = false;
public bool reverseScale = false;
private float spriteWidth = 0f;
 private Camera cam;
 private Transform myTransform;
private void Awake()
 {
 cam = Camera.main;
 myTransform = transform;
 }
// Use this for initialization
 void Start () {
 SpriteRenderer sRenderer = GetComponent<SpriteRenderer>();
 spriteWidth = sRenderer.sprite.bounds.size.x;
 }
 // Update is called once per frame
 void Update () {
 if (hasALeftBuddy == false || hasARightBuddy == false)
 {
 float camHorizontalExtend = cam.orthographicSize * Screen.width / Screen.height;
float edgeVisiblePositionRight = (myTransform.position.x + spriteWidth / 2) - camHorizontalExtend;
 float edgeVisiblePositionLeft = (myTransform.position.x - spriteWidth / 2) + camHorizontalExtend;
if (cam.transform.position.x >= edgeVisiblePositionRight - offsetX && hasARightBuddy == false)
 {
 MakeNewBuddy(1);
 hasARightBuddy = true;
 }
 else if (cam.transform.position.x <= edgeVisiblePositionLeft + offsetX && hasALeftBuddy == false)
 {
 MakeNewBuddy(-1);
 hasARightBuddy = true;
 }
 }
 }
void MakeNewBuddy (int rightOrLeft) {
 Vector3 newPosition = new Vector3(myTransform.position.x + spriteWidth * rightOrLeft, myTransform.position.y, myTransform.position.z);
 Transform newBuddy = Instantiate(myTransform, newPosition, myTransform.rotation) as Transform;
 if (reverseScale == true)
 {
 newBuddy.localScale = new Vector3(newBuddy.localScale.x * -1, newBuddy.localScale.y, newBuddy.localScale.z);
 }
newBuddy.parent = myTransform.parent;
 if (rightOrLeft > 0)
 {
 newBuddy.GetComponent<Tiling>().hasALeftBuddy = true;
 }
 else
 {
 newBuddy.GetComponent<Tiling>().hasARightBuddy = true;
 }
 }
}

these are my scripts.

is that enough info?

Show more replies
  • Liked by
Reply
Cancel