0% found this document useful (0 votes)
85 views5 pages

Falling Apart - Cameracontroller Script

The CameraControl script continuously moves the camera and environmental elements like backgrounds, walls, and borders as the camera moves up and down. It stores target positions for these elements and uses Lerp to smoothly transition between positions. When an element reaches the edge, it calls a switching method to swap the positions of two elements and allow continuous vertical looping movement.

Uploaded by

api-478656110
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
85 views5 pages

Falling Apart - Cameracontroller Script

The CameraControl script continuously moves the camera and environmental elements like backgrounds, walls, and borders as the camera moves up and down. It stores target positions for these elements and uses Lerp to smoothly transition between positions. When an element reaches the edge, it calls a switching method to swap the positions of two elements and allow continuous vertical looping movement.

Uploaded by

api-478656110
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Falling Apart – CameraController Script

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

public class CameraControl : MonoBehaviour


{
public Transform target;
public Transform bg1;
public Transform bg2;
public Transform w1a;
public Transform w1b;
public Transform w2a;
public Transform w2b;
public Transform b1a;
public Transform b1b;
public Transform b2a;
public Transform b2b;

private float sizeB;


private float sizeW1;
private float sizeW2;
private float sizeB1;
private float sizeB2;

//Initializing the location of the pieces of the environemnt (walls, building, camera)
private Vector3 cameraTargetPos = new Vector3();
private Vector3 bg1TargetPos = new Vector3();
private Vector3 bg2TargetPos = new Vector3();
private Vector3 w1aTargetPos = new Vector3();
private Vector3 w1bTargetPos = new Vector3();
private Vector3 w2aTargetPos = new Vector3();
private Vector3 w2bTargetPos = new Vector3();
private Vector3 b1aTargetPos = new Vector3();
private Vector3 b1bTargetPos = new Vector3();
private Vector3 b2aTargetPos = new Vector3();
private Vector3 b2bTargetPos = new Vector3();

void Start()
{
sizeB = bg1.GetComponent<BoxCollider2D>().size.y;
sizeW1 = w1a.GetComponent<BoxCollider2D>().size.y;
sizeW2 = w2a.GetComponent<BoxCollider2D>().size.y;
sizeB1 = b1a.GetComponent<BoxCollider2D>().size.y;
sizeB2 = b2a.GetComponent<BoxCollider2D>().size.y;

// Update is called once per frame


void Update()
{

private void FixedUpdate()


{

//Allows the virtically locked continuous movement of the camera


Vector3 targetPos = SetPos(cameraTargetPos, 0, target.position.y + 2.5f,
transform.position.z);

transform.position = Vector3.Lerp(transform.position, targetPos, 0.2f);

//Allows the continuous movement of the background imaages


if (transform.position.y >= bg2.position.y)
{
bg1.position = SetPos(bg1TargetPos, bg1.position.x, bg2.position.y + sizeB,
bg1.position.z);
SwitchBg();
}

if (transform.position.y < bg1.position.y)


{
bg2.position = SetPos(bg2TargetPos, bg2.position.x, bg1.position.y - sizeB,
bg2.position.z);
}

//Allows the continuous movement of the walls


if (transform.position.y >= w1b.position.y)
{
w1a.position = SetPos(w1aTargetPos, w1a.position.x, w1b.position.y + sizeW1,
w1a.position.z);
SwitchWalls1();
}

if (transform.position.y < w1a.position.y)


{
w1b.position = SetPos(w1bTargetPos, w1b.position.x, w1a.position.y - sizeW1,
w1b.position.z);
}

if (transform.position.y >= w2b.position.y)


{
w2a.position = SetPos(w2aTargetPos, w2a.position.x, w2b.position.y + sizeW2,
w2a.position.z);
SwitchWalls2();
}

if (transform.position.y < w2a.position.y)


{
w2b.position = SetPos(w2bTargetPos, w2b.position.x, w2a.position.y - sizeW2,
w2b.position.z);
}

//borders
if (transform.position.y >= b1b.position.y)
{
b1a.position = SetPos(b1aTargetPos, b1a.position.x, b1b.position.y + sizeB1,
b1a.position.z);
SwitchBorder1();
}

if (transform.position.y < b1a.position.y)


{
b1b.position = SetPos(b1bTargetPos, b1b.position.x, b1a.position.y - sizeB1,
b1b.position.z);
}

if (transform.position.y >= b2b.position.y)


{
b2a.position = SetPos(b2aTargetPos, b2a.position.x, b2b.position.y + sizeB2,
b2a.position.z);
SwitchBorder2();
}

if (transform.position.y < b2a.position.y)


{
b2b.position = SetPos(b2bTargetPos, b2b.position.x, b2a.position.y - sizeB2,
b2b.position.z);
}
}

public void SwitchBg()


{
Transform temp = bg1;
bg1 = bg2;
bg2 = temp;
}

public void SwitchWalls1()


{
Transform temp1 = w1a;
w1a = w1b;
w1b = temp1;
}

public void SwitchWalls2()


{
Transform temp2 = w2a;
w2a = w2b;
w2b = temp2;
}

public void SwitchBorder1()


{
Transform temp3 = b1a;
b1a = b1b;
b1b = temp3;
}

public void SwitchBorder2()


{
Transform temp4 = b2a;
b2a = b2b;
b2b = temp4;
}

private Vector3 SetPos(Vector3 pos, float x, float y, float z)


{
pos.x = x;
pos.y = y;
pos.z = z;
return pos;
}
}

You might also like