I created a spawn system and in the same script a collision scene transition system, but only the spawn works, the player is born in the desired location, but at the time of the scene transition he does not go to the desired location, he collides with the transition object, the player is destroyed from the current room and goes straight to spawn in another room and that’s not exactly what I want. I want the transition to go one place and the spawn to another.
follow the code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TransitionSpawn : MonoBehaviour
{
public Vector3 transPos;
public Vector3 startPos;
public GameObject playerPrefab = null;
public bool isSpawn;
public bool isTrans;
void Start()
{
isSpawn = true;
isTrans = false;
}
void Update()
{
}
void OnTriggerEnter(Collider other)
{
Destroy(GameObject.FindWithTag("Player"));
isTrans = true;
isSpawn = false;
}
void OnTriggerExit(Collider other)
{
if(isTrans == true ){
for (var i = 0; i < 1; i++)
{
Instantiate(playerPrefab, transPos, Quaternion.identity);
}
isTrans = false;
}else if((isSpawn == true) && (isTrans == false)){
for (var i = 0; i < 1; i++)
{
Instantiate(playerPrefab, startPos, Quaternion.identity);
}
}
}
}
It looks like you’re trying to use the OnTriggerEnter and OnTriggerExit events to handle spawning and transitioning your player character. However, it appears that you’re trying to do too much in these two methods.
One issue is that you’re using the OnTriggerEnter event to both destroy the current player object and set the isTrans flag to true. This means that when the player collides with the transition object, the player is immediately destroyed and the isTrans flag is set to true.
When the player exits the trigger, the OnTriggerExit method is called. Here, you check the isTrans flag to determine whether to instantiate a new player object at the transition position or at the spawn position. However, because the isTrans flag was already set to true in the OnTriggerEnter method, the player will always be spawned at the transition position, regardless of whether they actually entered the transition trigger or not.
To fix this, you could separate the logic for spawning and transitioning into separate methods. For example, you could create a SpawnPlayer method that takes a Vector3 position as an argument, and use this method to spawn the player at the desired position. You could then call this method from the OnTriggerEnter and OnTriggerExit methods to spawn the player at the appropriate position.
You can checkout my example:
public class TransitionSpawn : MonoBehaviour
{
// The position where the player should be spawned when they transition to a new scene
public Vector3 transPos;
// The position where the player should be spawned when they first enter the scene
public Vector3 startPos;
// The prefab for the player character
public GameObject playerPrefab = null;
// Whether the player is currently transitioning to a new scene
public bool isTransitioning = false;
void Start()
{
// Spawn the player at the starting position when the scene loads
SpawnPlayer(startPos);
}
void Update()
{
// You can add any additional logic for handling player spawning and transitioning here
}
void OnTriggerEnter(Collider other)
{
// Set the isTransitioning flag to true when the player enters the transition trigger
isTransitioning = true;
}
void OnTriggerExit(Collider other)
{
// If the player is currently transitioning, spawn them at the transition position
if (isTransitioning)
{
SpawnPlayer(transPos);
// Set the isTransitioning flag to false after the player has been spawned
isTransitioning = false;
}
}
// Method for spawning the player at a given position
void SpawnPlayer(Vector3 position)
{
// Destroy the current