- Home /
Following the newly created object
At the beginning I would like to apologize for my weak English
What's going on:
I'm making an Android app
I have a script "levelGenartor", it's obvious what is doing
Each of prefabs level has "childs"
The purple mark (empty Game.Object) is named and tagged as a "door", and it's generally a transition between levels
That's how it looks like:
The player has to move right or left depending where is a door. When someone touch the screen the "player" jumps to the next level. At this point, the next transition (door) have to be found - based on their position the vector of the movement will change.
I am totally new in unity (about 2 weeks) and I think that I don't have general knowledge about methods, functions etc. I tried many times with this but unfortunately with poor results. Any ideas ?
If you have to access the gameObject/script for the closest door to the player, I'd suggest having a list of references of all the doors sorted by distance to the player.
For example, an instance of List<DoorScript> doorsList
where all doors are referenced ( can be done via inspector or at runtime if your code generates them dynamically).
To access the closest one i'd do something like doorsList[0] and once the door has been used and the player moves to the next one, you can either control by having an index and do doorsList[currentDoor] or remove onces used by doing doorsList.RemoveAt(0) and doorsList[0];
@FernandoHC It's a good idea, I' ll try this later and I will say if it worked. In other words, "if I can do it at all." (ahh don't even know what I'm writing)
@misher // This is my "levelGenerator" code
private something rooms; //Reference to the GameObject Array (public GameObject[] platforms)
private int rand;
public float y_axis = 0.8f;
private int platformLimit = 0;
private Transform door;
void Start()
{
rooms = GameObject.FindGameObjectWithTag("rooms").GetComponent<something>();
}
void Update()
{
if(platformLimit < 10){
rand = Random.Range(0, rooms.platforms.Length);
Instantiate(rooms.platforms[rand], new Vector3(-12.2f, y_axis, 0), rooms.platforms[rand].transform.rotation);
y_axis += 1.2f;
platformLimit++;
}
}
Each of doors (dynamically created) has Tag: rooms attached to them
Please describe better the gameplay and question. I assume this: 1. You start with only player object, user can move it left or right and jump. 2. On Scene loaded, your level generator starts to generate "floors" (2 sections with a hole in between them at random position), 3. Generator only make several floors, then, only when player progress to the next floor, generator will add one more floor at the top (and destroy the bottom one) 4. When player jumps into the hole between 2 sections of the floor above, this should trigger progressing to the next floor (how exactly you have implemented that)
Yes exactly ! Everything as you said in addition to point 3. $$anonymous$$y script generated the floors infinitely and I had to limit it to 10 (btw. I want to do it as you said. In the sense that the camera moves higher, the floors at the top will be added and the bottom ones will be removed)
$$anonymous$$y problem is how to find a newly created object -> read it's position -> and on this basis, deter$$anonymous$$e the player direction vector
Answer by misher · Sep 05, 2018 at 01:25 PM
Moving camera infinitely is not a good idea, better to shift all floors down. When a player jumps into the hole between 2 segments of the floor it can hit a trigger, add collider to your "purple mark object" and flag it as trigger. Then in your Player script you can use this:
void OnTriggerEnter(Collider other)
{
Debug.Log(other.transform.position); // position of your trigger (floor transition market)
LevelManager.TransitionToNextFloor(); // proceed to nex floor (your custom logic)
}
Make sure to add rigidbody to your player object
Okay, I get it how can it work, but..
void OnTriggerEnter(Collider other)
{
Debug.Log(other.transform.position); // position of your trigger (floor transition market)
// Level$$anonymous$$anager.TransitionToNextFloor(); // proceed to nex floor (your custom logic)
}
When my "player" touches the door with: BoxColider2D and isTrigger = true: Nothing is shown in the console log. Only when Collision is detect position shows correct.
https://docs.unity3d.com/ScriptReference/$$anonymous$$onoBehaviour.OnTriggerEnter.html Do you have rigidbody component on your player object?
Yes, I have ! When isTrigger == false, then position is detected but also player doesn't move forward
Answer by telecaster · Sep 04, 2018 at 10:02 PM
Is there any reason why the generated doors could not be a prefab that has a script object on it? The script would simply at instantiation find any game object you wish to find and assign it to the script. You then can do whatever you want with either object in that script. Find in unity can be found here: https://docs.unity3d.com/ScriptReference/GameObject.Find.html
I don't really understand. Each of "door" would have a script Component (who does what?) GameObject.Find don't help a lot, because levels are created dynamically and has the same name :/
If I'm wrong - really sorry, but I don't get it :c
Your answer
Follow this Question
Related Questions
After i added proguard codes, I cannot connect to Google Play Service 0 Answers
Handling Multiple Aspect Ratios in Unity2D? 0 Answers
Transparency problem On phone 0 Answers
[5.4 - Unity2D - Android] UI Button Requires Child Text? 0 Answers
Tilemap Collider not working after build on android 0 Answers