- Home /
I'm trying to make platforms generate between two platforms which are already generated.
(Sorry but English is not my first language so there might be some errors but I will try to explain my problem in detail.)
I am working on my first 2D game and I was trying to make it a short platformer type game. I figured out how to make platforms spawn randomly by following this tutorial here. Link: https://www.youtube.com/watch?v=YznQRjbIpLU&list=PLiyfvmtjWC_XmdYfXm2i1AQ3lKrEPgc9-∈dex=8 (I followed upto the better jumping one, it uses object pooling method)
Then the next step was to implement a user interactable platform which I tried doing by myself by reading documents and maths. I was able to implement this for 2 fixed platforms and the code is attached below.
public class PlatformCreator : MonoBehaviour
{
[SerializeField]
GameObject refplat;
[SerializeField]
Transform pl1, pl2;
// Start is called before the first frame update
void Start()
{
createPlatform(pl1, pl2);
}
private void createPlatform(Transform pl1, Transform pl2)
{
float dist = Vector3.Distance(pl1.position, pl2.position);
float actualWidth = pl1.GetComponent<SpriteRenderer>().size.x;
float width1 = pl1.GetComponent<SpriteRenderer>().bounds.size.x;
float width2 = pl2.GetComponent<SpriteRenderer>().bounds.size.x;
float gap = dist - (width1 / 2 + width2 / 2);
Debug.Log(dist + " " + width1+" "+width2+" "+gap);
//calculate Position and width
Vector3 pPos = pl1.position + new Vector3((width1/2) +gap/2 , 10 , 0); // distance/2
GameObject g = Instantiate<GameObject>(refplat, pPos, Quaternion.identity);
g.transform.localScale = new Vector3((gap/actualWidth) + 0.3f,1,1);
}
Now the problem is that I couldn't figure out how to do this for all the platforms in my game as I couldn't figure out how to get the reference for the randomly generated platforms. Any help in this would be highly appreciated. Feel free to ask questions related to this.
Thank you in advance.
Your answer
Follow this Question
Related Questions
How can i create a 2D polygon map for my infinte runner game 0 Answers
2D Platform Player moving instantly from upper platform to lower, only when moving left 0 Answers
[2D Platformer] How do i make a platform that is only active when the player is standing on it? 1 Answer
Scale object, collision not working 2 Answers