Random Level Generation, unknown problem
Can someone tell me what's wrong with this code? I checked the position of the controller object that the script is attached to and it looks like it didn't move.
using UnityEngine; using System.Collections; using System.Collections.Generic; public class randomlevel : MonoBehaviour { public int randomness; public int lvl_size; private Vector3[] ways; void start () { Vector3[] ways = new Vector3[4] {new Vector3(0,0,5), new Vector3(0,0,-5), new Vector3(5,0,0), new Vector3(-5,0,0)}; int thatway = 0; List<Vector3> places = new List<Vector3> (); places.Add (transform.position); for (int i = 0; i <= lvl_size; i++) { transform.position = new Vector3 (transform.position.x + ways[thatway].x, 0, transform.position.z + ways[thatway].z); places.Add (transform.position); print("It works"); } } }
Note, the randomness property is not yet used, but once I get this problem solved I will add that part in. At this point the position of the controller object should end up at (0,0,25) because I set lvl_size to 5.
Any help would be appreciated.
Answer by wibble82 · Dec 21, 2015 at 05:21 PM
Hi
I'm not sure exactly what that code is supposed to do - you didn't mention that in your question! :)
There is an obvious issue though - you have called your function 'start', not 'Start'. C# is case sensitive, so your function won't be getting called at all automatically. If you're relying on that, then none of your code will be getting called.
I'm not sure where the 'print' function came from either?
After that, the code doesn't look like it will actually do a great deal of work - it is effectively just fulling in a list of vector3s by changing the transform of random level repeatedly. Maybe what you're looking for is something more along the lines of:
public class randomlevel : MonoBehaviour
{
public int randomness;
public int lvl_size;
//private Vector3[] ways; //this is redundant as we define a list of ways inside the start function!
// Start function gets called when randomlevel is initialized (note capital S!!)
void Start()
{
//our potential ways
Vector3[] ways = new Vector3[4] { new Vector3(0, 0, 5), new Vector3(0, 0, -5), new Vector3(5, 0, 0), new Vector3(-5, 0, 0) };
//our list of places
List<Vector3> places = new List<Vector3>();
//starting place is the transform of the randomlevel object?
Vector3 current_place = transform.position;
//add the first place
places.Add(current_place);
//add a set of extra places along a randomly generated path
for (int i = 0; i <= lvl_size; i++)
{
int thatway = 0; //replace 0 with random number between 0 and ways.Length (see Random.Range)
//add the 'way' vector to the current place to get the next position
current_place += ways[thatway];
//store current place in
places.Add(current_place);
//print something nice!
Debug.Log("Added place " + current_place.ToString());
}
//presumably now we'd do something with our places list?
}
}
That has a few floors, but might get you started :)
The "print" function just displays the text on the console. I'm not sure what the difference is between that and "Debug.Log" (I thought Debug.Log was just in Javascript, but it appears I'm wrong about that). And as for the script not really doing anything, that's because I couldn't actually write the more complicated code until the error was fixed. I'm not sure how I let such a simple mistake get past, but thanks! And I think I can figure out the "random" part myself, but if I need some help it will be nice to have the answer right here already.
No problem :) Do you want to mark it as 'right' then - just so others with similar questions or people looking for questions that need answering don't get confused!
Sorry it took me so long to mark your answer as "correct".