Hi!!!
I want my player start on different locations everytime I start a new game!
How do I do that?
Just now my player is in the game at the same startpoint!
Thanks for help!!!
Hi!!!
I want my player start on different locations everytime I start a new game!
How do I do that?
Just now my player is in the game at the same startpoint!
Thanks for help!!!
What have you tried so far? If your map is flat and empty, you could use a random number generator to set your X and Z coordinates to somewhere within the bounds of the map and then move your player there. If your map is not empty, you’ll have to double-check you’re not instantiating on top of something else, and if so, retry. If your map is not flat, you’ll have to decide on an appropriate height to spawn at.
Personally, I would try to avoid this complexity, and simply pre-create a half-dozen possible spawn points.
That may be “random” enough.
Hi! Thanks for awnsering!
My map is flat with many rooms!
Ive got this code from Bravini, but the code don´t work!
var xpos : int;
var ypos : int;
var zpos : int;
var PlayerPrefab : Transform;
function Start () {
//set this to the area you want the player to appear according to the size of your map xpos = Random.Range(1,100); ypos = Random.Range(1,100); zpos = Random.Range(1,100);
Instantiate(PlayerPrefab, transform.position(xpos,ypos,zpos), (transform.rotation(0,0,0));
}
Make empty game object with tag “Spawn point”. Get your component / script that make your player spawn. Inside the Reset function, write stuff to find and return an array of all game object in the scene with the tag “Spawn point”. Use then Random.Range function to get a random number, and a random spawn point.
I already gave my preference for spawnpoints, but let’s focus on why your code doesn’t work…
xpos = Random.Range(1,100); ypos = Random.Range(1,100); zpos = Random.Range(1,100);
That’s the problem line. I doubt you want to spawn at a random point between (1,1,1) and (100,100,100). You want to spawn at a random point on the ground within the bounds of your gameworld!
For example, if the ground of your map was a plane stretching from (-9, -0.5, -9) to (8, 0.5, 8), you need to write code that looks closer to the below. ypos shouldn’t be random at all if your map is flat!
xpos = Random.Range(-8,8); ypos = 1; zpos = Random.Range(-8,8)
The key is, you need to know the bounds of your map, and update the values accordingly! Here is some sample code that’s working fine for me. If you really wanted to work on this as a solution, you will need to add code to ensure you’re not instantiating on top of another object, and ideally you’d figure out the ground’s bounding box dynamically. Pre-creating a few spawn points avoids this complexity.
using UnityEngine;
using System.Collections;
public class PlayerCreator : MonoBehaviour {
public Transform player;
private Vector3 pos;
// Use this for initialization
void Start () {
// Choose the player position from somewhere random on the map.
pos.x = Random.Range(-8,8);
pos.y = 1;
pos.z = Random.Range(-8,8);
GameObject.Instantiate( player, pos, player.transform.rotation );
}
}
Hi! Ostagar!!!
Thankyou it works!!!
Ok about using spawn point, I think that would be better like you sad.
I have made spawn point gameobject but I don´t know how to make the array script to that?
Thank you for repley!!!
Well, Akilae basically explained that solution, but I’ll break it down a bit further.
Actually, make more than one. If you want six spawn points, make six of them. You’ll probably want to put them in a folder so you don’t flood your project with spawn point objects.
The procedure you’ll want is GameObject.FindGameObjectsWithTag(). It’s a static member function, meaning you’re calling the function on the class itself, rather than on a specific instance.
Arrays in C# are made with [ ]. Eg, int [ ] myArray = new int[6].
If this sounds tricky, you may want to find a C# or UnityScript tutorial.
You’ve already seen how to use Random.Range to select a value between X and Y. The only trick here is remembering arrays are 0-based. Don’t make the mistake of searching from 1 to 6!
Hi Ostagar!
Can´t find an example in C# that can show me how to do it!
Do you have an example? If you have it would be the best thing of the day!
Thanks in advance!!!
I had the same problem once and came up with the scipt below. Basically, this lets you define a “spawn area” through a texture and the player will spawn on a random point (on the surface, never below or in the air) within the places you painted white. You probably have to adapt it to your needs, but it should give you the basics.
/*
Spawn the player at a random point within defined area
(C)2009 Tom Vogt <tom@lemuria.org>
*/
using UnityEngine;
using System.Collections;
public class SpawnInArea : MonoBehaviour {
public Texture2D SpawnMap;
float Offset = 10.0f;
float AboveGround = 1.0f;
bool TerrainOnly = true;
// Use this for initialization
void RandomPositionOnTerrain(GameObject obj) {
Vector3 size = Terrain.activeTerrain.terrainData.size;
Vector3 NewPosition = new Vector3();
bool done = false;
while (!done) {
// calculate new random position
NewPosition = Terrain.activeTerrain.transform.position;
float w = Random.Range(0.0f, size.x);
float h = Random.Range(0.0f, size.z);
NewPosition.x += w;
NewPosition.y += size.y + Offset; // make sure we are above the terrain
NewPosition.z += h;
// verify that position is in spawnmap
if (SpawnMap) {
int xmap = Mathf.RoundToInt((float)SpawnMap.width * w/size.x);
int ymap = Mathf.RoundToInt((float)SpawnMap.height * h/size.z);
float value = SpawnMap.GetPixel(xmap, ymap).grayscale;
if (value>0.0f Random.Range(0.0f, 1.0f)<value) {
done = true;
} else {
done = false;
}
} else {
done = true;
}
if (done) {
// verify that position is above terrain/something
RaycastHit hit;
if (Physics.Raycast(NewPosition, -Vector3.up, out hit)) {
float distanceToGround = hit.distance;
if (hit.transform.name != "Terrain") {
if (TerrainOnly) done = false; // there is something else beneath us
}
// all is good
NewPosition.y -= (distanceToGround-AboveGround);
} else {
done = false; // there's nothing under us as far as we care to check
}
}
}
// put the object in place
obj.transform.position = NewPosition;
// also add random rotation
transform.Rotate(Vector3.up * Random.Range(0, 360), Space.World);
}
}