- Home /
How to connect two objects so when one respawn the other also does?
I am making a game where i am moving two players at once. How can i connect them so if one respawn the other also respawn. Is there a component that can do that or do i have to change my respawn script? thx in advance.
Answer by tormentoarmagedoom · Mar 05, 2020 at 06:19 PM
Hello.
You need to do it by code of course..
In Object A, check if object B is respawned, if not, do it.
in Object B check if object A is respawned, if not, do it.
Update
I asked you where is the script attached! What object have the trigger collider to detect the trigger enter? Its the player itself? Its the object that kills the player?
I supose is in the object that kills the player (thats why you need to say that want to change the player transform)
I see you need some more things before ocntinue. First. Your OnTrigger function is now activateing will all other colliders that enters inside that trigger collider (anything with a collider will activate it)
You need to specify which colliders make player respawn, something like this (Code may have typing errors):
void OnTriggerStay(Collider other)
{
if (other.name.contains("Player")
{
//Respawn function
}
}
So only Players will activate the respawn.
And second, instead of redspawinign only one player, make both players respawn
void OnTriggerStay(Collider other)
{
if (other.name.contains("Player")
{
//Respawn Player 1
//Respawn Player 2
}
}
I see you are still very "noobie". I strongly recommend you to spend next 5-10 hours looking at tutorials for complete games. Dont try to go fast, you need to learn step by step. Dont worry if dont understand a lot of things, just try to understand and try all you can before continue with your project.
You are in the correct way!
Good luck!
Thx for the answer. Do i have to incorporate it into my existing respawn script or do i have to make a new code that checks if my respawn script has been used on my object? I am still new to coding so i might not understand what you are saying if it gets too technical. Thx in advance.
HOw are you doing the respawn? where is the script attached?
$$anonymous$$aybe best way is to have 1 script in each character, and when he dies, activate the respawn funcion of the other character too.
$$anonymous$$y respawn script is this using UnityEngine; using System.Collections;
public class Respawn : $$anonymous$$onoBehaviour { [SerializeField] private Transform player;
[SerializeField]
private Transform respawnpoint;
private void OnTriggerEnter2D(Collider2D other)
{
player.transform.position = respawnpoint.transform.position;
}
}
and i attach it to my enemy
Your answer
