- Home /
Recommendations on how to load characters...
My google-fu is weak on this topic. I searched for a while and wasn't able to find anything. Possibly because I'm searching for the wrong terms.
I have a scene that I want to load the player's characters on to. The scene has a loading "zone" or area where the characters can be loaded.
My first thought was to place a plane on the scene that would be the "loading" plane. Loop through the characters and place them on that plane. However, I haven't been able to figure out how to get the dimensions of the plane in Javascript.
Am I going about this the right way?
Answer by Adam-Buckner · Jan 30, 2013 at 10:17 AM
I have made this a "new" answer, as the comment is hidden under the "show more replies". I thought Answers had failed to save it as I missed the tiny button for "show more replies", so I wrote it twice.
Well, we're even then! (^_^)!
I guess what I'm saying is that you don't need to know the dimensions of the plane. To get an area (your spawn zone) you need to know a point and the dimensions or area around it. What I'm saying is you don't need the plane. Replace the plane with a point. You need a point (GameObject Transform) - and then an x (float) value for a square or an x,z (Vector2) value for a rectangle.
You could find the bounds of the mesh you are using. Look up BOUNDS in the docs. But if you read the description, you can see that bounds is made up of its center and extents, or alternatively by min and max points. This is what you get by using the Spawn Point game object and a value, and you will have more control over that in the inspector than by manipulating a plane.
If you need to see a plane in the scene view for reference purposes, you can use a plane attached to your spawn point, but you will need to find a way to remove/deactivate your plane at run time (see above):
If it were me I would use Gizmos:
The quick back-pocket code for the gizmos is here:
@script ExecuteInEditMode()
var spawnArea : float;
private var frontLeft : Vector3;
private var frontRight : Vector3;
private var backLeft : Vector3;
private var backRight : Vector3;
function Start () {
spawnRadius = spawnArea / 2;
}
function Update () {
frontLeft = Vector3 (transform.position.x + spawnRadius, transform.position.y, transform.position.z + -spawnRadius);
frontRight = Vector3 (transform.position.x + spawnRadius, transform.position.y, transform.position.z + spawnRadius);
backLeft = Vector3 (transform.position.x + -spawnRadius, transform.position.y, transform.position.z + -spawnRadius);
backRight = Vector3 (transform.position.x + -spawnRadius, transform.position.y, transform.position.z + spawnRadius);
}
function OnDrawGizmos () {
Gizmos.color = Color.red;
Gizmos.DrawLine(frontLeft, frontRight);
Gizmos.DrawLine(frontRight, backRight);
Gizmos.DrawLine(backRight, backLeft);
Gizmos.DrawLine(backLeft, frontLeft);
Gizmos.DrawIcon (transform.position, "SpawnPoint.psd", true);
}
So - yes, you could use a plane and get the bounds on the mesh, but you could be tying your hands when it comes to the versatility of creating an area and maintaining it in the inspector.
Looking at all the green in the scene it might be better that you change:
Gizmos.color = Color.green;
to
Gizmos.color = Color.red;
Perfect. Thanks.
The reason I wanted to use a plane, and not a random point within a range of a point is for loading people inside a room. Say the zone in area is inside a house, I thought it would be easier to lock players zoning in to a square plane in the house rather than a random point inside that house.
Little Angel, thank you so much. Thanks for making this community an amazing resource.
Answer by Adam-Buckner · Jan 29, 2013 at 03:18 PM
I would def. search on this one. It has been answered a number of times. Try here for a google custom search engine for everything Unity: Looking for Answers? Use the Google Custom Search Engine!
TBH, I'm a little unclear what you are asking about. Is this a multi-player game? And why are you loading characters at runtime?
If you want to "spawn" characters, I would create a "Spawn Point" for them.
Read up on Instantiate in the docs and notice that you need an object, vector3 and quaternion to instantiate something - or what you want to instantiate, where you want to instantiate it and lastly, what the rotation for that object will be. A GameObject's Transform Component will have the Vector3 as Position and the Quaternion as Rotation, so you can create a "Spawn Point" using an empty GameObject and you can reference that with a variable of type Transform:
var mySpawnPoint : Transform; // JS
public Transform mySpawnPoint; // C#
You could then create a number of spawn points, save them in a list or array and pick one at random, or, if it works for your game, take that one spawn point and then add a random value to the position so when you instantiate your character it will be "near" that spawn point:
spawnPosition : Vector3 = Vector3 (mySpawnPoint.x + Random.Range (-1, 1), mySpawnPoint.y, mySpawnPoint.z + Random.Range (-1, 1));
You will need to be more clever than that to prevent multiple spawns from potentially overlapping.
It is not a multiplayer game.
The squad is loaded on scene load, randomly in the scene, but in a limited area. Think, random encounter, of a variable sized squad. I don't want to have to go throw every random encounter scene and make spawn points for 1-(insert maximum size of squad).
Ins$$anonymous$$d, I want to put a 10x10 plane on every scene, and load characters into that zone/plane when the random encouter kicks off.
You say I should search for an answer, but term should I search for. I searched for "loading object on a plane", "getting (dimension OR location) of a plane", "instantiating gameobject on a plane", and so on. I think the problem is, I don't know how to phrase what I'm actually trying to do, in order to find good results.
I have converted your "answer" to a "comment". (^_^) Remember, Answers is different from the forum, so comments should be added to the proposed Answer or to the Original Question unless they are an answer.
Fudge... Answers buggered the formatting and I went to fix it and it seems to have been deleted.
13 $$anonymous$$utes between comments... lost
I despise the Answers back end.
FUDGE!
So ...
You can essentially do what I suggested above.
If you want to spawn your squad in a 10x10 square you can do it with this:
for (var i : int = 0; i < squadSize; i++) {
var spawnPosition : Vector3 = Vector3 (
mySpawnPoint.x + Random.Range (-5, 5),
mySpawnPoint.y,
mySpawnPoint.z + Random.Range (-5, 5));
var spawnRotation : Quaternion = SetNewFacing (mySpawnPoint);
Instantiate (squaddiePrefab, spawnPosition, spawnRotation);
// SetNewFacing (t : Transform) is a simple placeholder for a random heading
}
If you want the plane as a way of indicating the spawn point in the scene view during edit time, you could either include a plane or a cube to act as a reference to where the spawn point would be - just attach a script that destroys it or sets it inactive.
function Start () {
Destroy(gameObject);
}
function Start () {
gameObject.SetActive (false);
}
You could also look into Gizmos and OnDrawGizmos ins$$anonymous$$d, as they are designed to draw things like icons and lines in the editor, so you could make a gizmo line box around the spawn point.
This system fails "out of the box" in the same places that using a plane would fail:
You could instantiate squaddies so their geometry overlapped or they were "just too close" to be comfortable - you would need code to check and adjust for this.
You could instantiate squaddies so they overlapped scene items or geometry - you would need code to check and adjust for this.
You could have uneven terrain - you would need to not only instantiate them, but test for where the ground is, and place them on the ground.
But this would be true of the plane solution as well.
Ah, thanks for the info on answer/comment.
Thank you so much for the info, unfortunately, you've skipped the only question I actually have.
How do I get the location/dimensions of a plane? I already have everything written that you have mentioned, except being able to place the squad on the plane. I can place them at random vectors, and predefined vectors, but I can't place them on the "zone-in" plane because I don't know how to find a plane's location and dimensions with javascript.
Thanks.
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Random location when clicked on 1 Answer
character in Reverb Zone? 1 Answer
Load character from character selector to the game scene 1 Answer