- Home /
Question by
unity_yhBKvPzfmrqe9g · May 12 at 08:17 PM ·
scripting problemphysicsinstantiatetransformprogramming
My objects start flying after I Instantiate them
Hello. The first script is the one that every tile has and the second one is about the game logic. I hope the script is readable enough. After the first 6 tile lines that get generated in the start function and another set that gets generated after the furthest back set of tiles get destroyed the next tiles start flying at lightning speed in the opposite direction and drag back every tile generated after the start function :/ IF THERE IS ANYTHING PLEASE LET ME KNOW SO I CAN EXPLAIN IT
THANK YOU VERY MUCH!!!!!
Here is the tile prefab :D
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TDR_Platforms : MonoBehaviour
{
[SerializeField]
GameObject gameLogic;
[SerializeField]
float speed;
public float platformLifetime;
public GameObject platformGenerated;
TDR_GameLogic myGL;
// Start is called before the first frame update
void Start()
{
myGL = gameLogic.GetComponent<TDR_GameLogic>();
}
// Update is called once per frame
void Update()
{
PlatformMovement();
if(Time.time > platformLifetime)
PlatformBehaivour();
}
private void OnCollisionEnter(Collision collision)
{
Debug.Log("M-am Belit");
}
private void PlatformMovement()
{
transform.position += Vector3.back * (speed * Time.deltaTime);
}
private void PlatformBehaivour()
{
myGL.AddLastPlatform();
if (this.gameObject.tag == "MidPlatform")
{
myGL.PlatformGeneration();
}
Destroy(this.gameObject);
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TDR_GameLogic : MonoBehaviour
{
[SerializeField]
GameObject player;
[SerializeField]
Vector3 startingPosition;
[SerializeField]
GameObject platform;
[SerializeField]
int startingTiles;
[SerializeField]
Vector3 midPlatofrmFirstPosition;
[SerializeField]
float platformLifetime;
[SerializeField]
GameObject lastPlatformGenerated;
Vector3 nextSpawn;
GameObject midPlatform;
int timeToAdd = 0;
GameObject platformGenerated;
GameObject[] platformsInScene;
// Start is called before the first frame update
void Start()
{
GenerateStartWorld();
}
private void GenerateStartWorld()
{
nextSpawn = midPlatofrmFirstPosition;
Instantiate(player, startingPosition, Quaternion.identity);
for(int i = 1; i<=startingTiles; i++)
{
midPlatform = Instantiate(platform, nextSpawn, Quaternion.identity);
midPlatform.tag = "MidPlatform";
midPlatform.GetComponent<TDR_Platforms>().platformLifetime = platformLifetime + timeToAdd;
nextSpawn.x = -10.001f;
lastPlatformGenerated = Instantiate(platform, nextSpawn, Quaternion.identity);
lastPlatformGenerated.GetComponent<TDR_Platforms>().platformLifetime = platformLifetime + timeToAdd;
nextSpawn.x = 10.001f;
lastPlatformGenerated = Instantiate(platform, nextSpawn, Quaternion.identity);
lastPlatformGenerated.GetComponent<TDR_Platforms>().platformLifetime = platformLifetime + timeToAdd;
nextSpawn.z = nextSpawn.z + 10.001f;
nextSpawn.x = 0;
timeToAdd += 5;
//AddLastPlatform();
}
}
// Update is called once per frame
void Update()
{
}
public void PlatformGeneration()
{
timeToAdd = 10;
nextSpawn = lastPlatformGenerated.transform.position;
nextSpawn.z += 9.98f;
nextSpawn.x = 0;
midPlatform = Instantiate(platform, nextSpawn, Quaternion.identity);
midPlatform.GetComponent<TDR_Platforms>().platformLifetime = platformLifetime + timeToAdd;
midPlatform.gameObject.tag = "MidPlatform";
nextSpawn.x = -10;
platformGenerated = Instantiate(platform, nextSpawn, Quaternion.identity);
platformGenerated.GetComponent<TDR_Platforms>().platformLifetime = platformLifetime + timeToAdd;
platformGenerated.gameObject.tag = "Tile";
nextSpawn.x = 10;
platformGenerated = Instantiate(platform, nextSpawn, Quaternion.identity);
platformGenerated.GetComponent<TDR_Platforms>().platformLifetime = platformLifetime + timeToAdd;
platformGenerated.gameObject.tag = "Tile";
nextSpawn = Vector3.zero;
}
public void AddLastPlatform()
{
platformsInScene = GameObject.FindGameObjectsWithTag("Tile");
foreach(GameObject possiblePlatform in platformsInScene)
{
float maxZ = 0;
if(possiblePlatform.transform.position.z > maxZ)
{
maxZ = possiblePlatform.transform.position.z;
lastPlatformGenerated = possiblePlatform;
}
}
}
}
image-2022-05-12-231714820.png
(232.9 kB)
image-2022-05-12-231736239.png
(206.4 kB)
Comment