- Home /
Multiple Spawn Points for fps character
Hi so Im attempting to make an fps with a maze for a map, and I'm wanting to make multiple character spawning points in the same room(scene), depending on which room(scene) you previously came from. So with a maze say in one room you have 4 different doors leading to it, and depending on which room you exited from, you will need a different spawn point, so as to not be confused on where you come from. I have done some very basic coding, but this is little beyond me, and need some guidance. I have a "DoorManager" script that persists through different scenes, although im not sure I will need it or not.
I would like to say something like if ive left room A, then spawn in room B @ thispoint else if i've left room C, then spawn in room B @ this point
so it would be like if ive gone from one specific scene to another specific scene, i would respawn at this specific point.
Ive also considered using empty objects and tags, so setting the individual empties infront of the doors, depending on which one i trigger, the next scene i would spawn a specific place corresponding with said trigger?
I hope these help, but if anyone has ideas pleas share them, also in your code pls be a detailed as possible, not like to an unnecessary amount i just can't always interpolate the information into workable C# bc im not well versed in it yet
Thank you sm!!!!!
Answer by jackmw94 · Nov 10, 2020 at 01:03 PM
If you door is as simple as 'if player enters this doorway, teleport them to a new position' then you could just have a script like this:
public class Door : MonoBehaviour
{
[SerializeField] private Transform _doorTarget;
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.name.Equals("Player"))
{
other.transform.position = _doorTarget.transform.position;
}
}
}
This would require the gameobject to have a collider on it - an easy way to set this up would be to make a cube, add this script then disable the mesh renderer once you've repositioned and resized it.
The if-statement could be any check that determines whether the object that's entered the door is the player object; could check whether the object has a player-specific script on or whether it has a certain tag. If you're using this name check then make sure to change "Player" to whatever your player is called!
If your character has a character controller on it then this will disrupt manual positioning like we're doing above so you can alter it like so:
private void OnTriggerEnter(Collider other)
{
var characterController = other.GetComponent<CharacterController>();
if (characterController)
{
characterController.Move(_doorTarget.transform.position - other.transform.position);
}
}