Tiling Script in 2D Platformer

Updated on September 27, 2018 in [C] Beginner
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
2 on September 24, 2018

I have been watching How tomake 2D platformer https://www.youtube.com/watch?v=77zdOaUGguc and tried the script on my laptop with the same code that was shown in the video. After Completing all the code i run my game but ground that is creating is having a lot of gap with between one another. I am facing a challenge here can anyone suggest me what i did wrong.

 

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

[RequireComponent (typeof(SpriteRenderer))]

public class Tiling : MonoBehaviour {

public int offsetX = 2; // Offset so that we dont get errors

// these are used to check if we need to initiate stuff
public bool hasARightBuddy = false;
public bool hasALeftBuddy = false;

//used if object is not tilable
public bool reverseScale = false;

// the width of our element
private float spriteWidth = 0f;

private Camera cam;
private Transform myTransform;
//public Transform parents;

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 ()
{
// does it still needs buddies if not do nothing
if(hasALeftBuddy == false || hasARightBuddy == false){
// Calculate camera’s extent (half of width) of what camera can see in world coordinates
float camHorizontalExtend = cam.orthographicSize * Screen.width / Screen.height;

// Calculate the x position where the camera can see the edge of the sprite
float edgeVisiblePostionRight = (myTransform.position.x + spriteWidth / 2) – camHorizontalExtend;
float edgeVisiblePostionLeft = (myTransform.position.x – spriteWidth / 2) + camHorizontalExtend;

// checking if we can see edge of the element and calling MakeNewBuddy if we can
if (cam.transform.position.x >= edgeVisiblePostionRight – offsetX && hasARightBuddy == false)
{
MakeNewBuddy(1);
hasARightBuddy = true;
}
else if (cam.transform.position.x <= edgeVisiblePostionLeft + offsetX && hasALeftBuddy == false)
{
MakeNewBuddy(-1);
hasALeftBuddy = true;
}
}
}

// A function that creates a buddy on the side required

void MakeNewBuddy(int rightorleft)
{
// calculating new position for our buddy
Vector3 newPosition = new Vector3(myTransform.transform.position.x + spriteWidth * rightorleft, myTransform.position.y, myTransform.position.z);

// we are instantiating a new buddy and storing in a variable.
Transform newBuddy = Instantiate(myTransform, newPosition, myTransform.rotation) as Transform;

// if not tilable let’s reverse the x size of our object to get rid of ugly seams
if (reverseScale == true)
{
newBuddy.localScale = new Vector3(newBuddy.localScale.x * -1, newBuddy.localScale.y, newBuddy.localScale.z);
}

newBuddy.parent = myTransform.parent;

//newBuddy.transform.parent = parents.transform;

if (rightorleft > 0)
{
newBuddy.GetComponent<Tiling>().hasALeftBuddy = true;
}
else
{
newBuddy.GetComponent<Tiling>().hasARightBuddy = true;
}
}
}

  • Liked by
Reply
0 on September 26, 2018

This is fixed in video 6 of the series.

  • Liked by
Reply
Cancel
0 on September 27, 2018

thanks

  • Liked by
Reply
Cancel