- Home /
Temporary GameObject Movement
PLEASE READ ALL OF THE INFO AS IT IS IMPORTANT THAT YOU KNOW ALL THE INFO ABOUT THE SCRIPT FIRST!
Hello everyone, I am currently in the process of making a small unity game all alone. Right now there is a big bottleneck in my project which if I solve, the game will be pretty much 99% done. This has been bothering me for a week now.
It is about a script which makes the lowest of multiple objects on screen horizontally controllable, sort of like tetris where the lowest group of blocks is always controllable and as soon as the lowest controllable objects disappears, the next lowest one turns into the controllable and so on. I havent tried making a queue script, since I am unexperienced when it comes to . But, here is a script that I compiled with the help of several reddit users, which still seems to be not working.
Note that no compiler errors appear, but the functions just dont work...
This script here has a movement mechanic (which works fine whenever I manually select the lowest object), a spawn mechanic (which doesnt work) and a mechanic that is supposed to track the lowest object on the screen out of the listed ones (doesnt work). the part where it says redbrix, bluebrix, these are all different gameobjects that are supposed to be moveable (the lowest one on screen is the moveable one), the script is supposed to track all these 5 objects and define which one is the lowest on screen and add it to the "currentObject", but that just doesnt work... this script is added to the mainCamera.
SCRIPT STARTS HERE
using UnityEngine; using System.Collections; using System.Collections.Generic;
public class controllerList : MonoBehaviour { public List ControlObjects = new List(); // Tracks all the objects current on screen public GameObject CurrentObject; // Tracks the current controlled object
// References to the objects to instantiate
[SerializeField] private GameObject redbrix;
[SerializeField] private GameObject bluebrix;
[SerializeField] private GameObject yellowbrix;
[SerializeField] private GameObject greenbrix;
[SerializeField] private GameObject rainbowbrix;
// "delay" the amount of delay in seconds between the gameObjects spawning
[SerializeField] private float delay = 1f;
void Start()
{
InvokeRepeating("Spawn", delay, delay);
}
public void Update()
{
// Input controls to move the CurrentObject
if (Input.GetKeyDown(KeyCode.A))
{
Vector3 position = CurrentObject.transform.position;
position.x--;
CurrentObject.transform.position = position;
}
if (Input.GetKeyDown(KeyCode.D))
{
Vector3 position = CurrentObject.transform.position;
position.x++;
CurrentObject.transform.position = position;
}
}
/*---------------------------
// Spawns a new brix at the spawn point
// -- brixType: the type of brix you want to spawn
-----------------------------*/
public void SpawnNewObject (GameObject CurrentObject)
{
// Create the new brix
GameObject newBrix = Instantiate(CurrentObject, new Vector3(Random.Range(-8, 8), 6, 0), Quaternion.identity) as GameObject;
// Add the new brix to the list of objects
ControlObjects.Add(newBrix);
}
/*---------------------------
// Will find the lowest brix in the scene and set it to CurrentObject
-----------------------------*/
public void GetLowestObject()
{
// This temporary variable will track the lowest object, set to the first object by default
GameObject lowestObject = ControlObjects[0];
// Now loop through the rest and check if it's lower
for (int i = 1; i < ControlObjects.Count; i++)
{
if (ControlObjects[i].transform.position.y < lowestObject.transform.position.y)
lowestObject = ControlObjects[i];
}
}
/*---------------------------
// Call this when you want the current controlled object to be destroyed
-----------------------------*/
//public void DestroyCurrentObject()
//{
// ControlObjects.Remove(CurrentObject);
// Destroy(CurrentObject);
//}
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
How can i make my character jump and slide on command? 0 Answers
Movement Code not working 2 Answers
How to I put a speed limit on my character's movement? 1 Answer