- Home /
Randomize the position of the player
I would like to ask how to randomise the position of the player once the network is loaded. The code snippet below is what i have done to randomise the position of the player but it doesn't seem to work. All the players are spawn at the same position. Can i know what wrong with the code?
function OnNetworkLoadedLevel()
{
// Randomize starting location
var spawnpoints : GameObject[] = GameObject.FindGameObjectsWithTag ("Spawnpoint");
Debug.Log("spawns: "+spawnpoints.length);
var spawnpoint : Transform = spawnpoints[Random.Range(0, spawnpoints.length)].transform;
var newTrans : Transform = Network.Instantiate(playerPrefab,spawnpoint.position, spawnpoint.rotation, 0);
}
Answer by J8Ptt · Mar 13, 2012 at 05:26 PM
Random.Range() returns a float value, but you are using it as an array index (which is an int value). Maybe that's the problem? Try rounding the output of random:
var spawnpoint : Transform = spawnpoints[Mathf.RoundToInt(Random.Range(0, spawpoints.length))].transform;
Does it work?
No.. It still spawn at the same position. anyway i got an error for this line: var newTrans : Transform = Network.Instantiate(playerPrefab,spawnpoint.position, spawnpoint.rotation, 0);
InvalidCastException: Cannot cast from source type to destination type. GameSetup.OnNetworkLoadedLevel () (at Assets/Scripts/$$anonymous$$ultiPlayerScripts/GameSetup.js:144) UnityEngine.GameObject:Send$$anonymous$$essage(String, Send$$anonymous$$essageOptions) GameSetup:Awake() (at Assets/Scripts/$$anonymous$$ultiPlayerScripts/GameSetup.js:44)
That's a casting error. Try explicitly cast the result of the "Instantiate" to Transform:
var newTrans : Transform = (Transform) Network.Instantiate(playerPrefab,spawnpoint.position, spawnpoint.rotation, 0);
About the random, are you sure that the "spawpoints" array has something? I can't see anything wrong with the code, so I suggest you double-check if your spawn points are correctly labeled.
I believe J8$$anonymous$$t is probably right that the FindGameObjectsWithTag is not actually returning anything to the array ... in your Debug.Log, are you getting the .length you expect? If not, like J8$$anonymous$$t said, you've probably forgotten to tag you spawn points with "Spawnpoint".
As a side note, J8Ptt, Random.Range returns an int when given int parameters. I use it for returning random array elements all the time.
If missypooh's code is spawning everyone at the same position, with the code above, it means there's at least one Spawnpoint. I'd guess there isn't more than one, though.
Answer by syclamoth · Mar 14, 2012 at 02:51 AM
If you want to spawn in a random position, try using something like this:
// having determined the spawnPoint, get a random point in a circle:
var randomCircle : Vector2 = Random.insideUnitCircle;
var spawnOffset : Vector3 = new Vector3(randomCircle.x, 0, randomCircle.y);
var newTrans : Transform = Network.Instantiate(playerPrefab,spawnpoint.position + spawnOffset, spawnpoint.rotation, 0);
Otherwise, if you want to have a large number of spawnPoints, you can either collect them with the use of a common tag, and use the code that you posted in your question (make sure that all the transforms that you are using as spawn points have the "Spawnpoint" tag), or you can manually define them by dragging them into an array in the inspector:
// right at the top!
public var spawnPoints : Transform[];
// then when you go to use it:
var spawnPoint : Transform = spawnPoints[Random.Range(0, spawnPoints.Length)];
If you want to use both, there's no reason why you can't apply the position randomisation after having chosen a spawnPoint!
hi, i try using the one on spawnPoints: Transform; But my player fall infinitely..
This is what i do.. correct me if i am wrong.
First i create 5 spawnpoints.
Then in the inspector, i initialize on the spawnpoints.
The script is attached to the empty gameObject not on the spawnpoint. So is there anything wrong?
Your answer
![](https://koobas.hobune.stream/wayback/20220613065100im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Unity networking tutorial? 6 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
what should be considered first in multiplayer game 1 Answer
Set unity process as server and client? 0 Answers
Multiplayer LAN game? 0 Answers