- Home /
[solved] Having a problem with respawning script. the player didn't go to the proper respawn point
Hi! I'm making a game that when the player dies, he'll go to the spawn point that is an empty game object. the problem is when the player dies, he goes to the actual spawn point for just single frame and then bounce back to the death point(in front of the enemy). not very sure where's the problem came from.
it's the line below the one that said "You shall respawn now" that replaces current player position and rotation with that of the respawn point.
This is my code for the respawning.
void Update()
{
if (playerIsDead==true)
{
FirstPersonController FPScon = GetComponent<FirstPersonController>();
FPScon.enabled = false;
melee.SetActive(false);
}
}
public void OnGUI()
{
if (playerIsDead==true)
{
Cursor.visible = true;
Cursor.lockState = CursorLockMode.None;
if (GUI.Button(new Rect(Screen.width * 0.5f - 50, 180, 100, 40), "Respawn")) { respawnPlayer(); }
if (GUI.Button(new Rect(Screen.width * 0.5f - 50, 240, 100, 40), "Menu")) { Debug.Log("Return to Menu"); }
}
}
public void respawnPlayer() {
Debug.Log("You Shall Respawn Now");
transform.position =respawnTransform.position;
transform.rotation =respawnTransform.rotation;
gameObject.SendMessage("RespawnStats");
FirstPersonController FPScon = GetComponent<FirstPersonController>();
FPScon.enabled = true;
melee.SetActive(true);
playerIsDead = false;
resurrected = true;
}
}
Answer by kasuyagi · Mar 30, 2019 at 03:55 PM
omg I figured it out.
the main problem is the character controller of the player. it has an inner mechanism that will ignore transform.position = new position so... to do this i had to turn off the character controller before changing the position and then after that, i turned character controller on once again
the code will look like this
GetComponent<CharacterController>().enabled = false;
transform.position =respawnTransform.position;
transform.rotation =respawnTransform.rotation;
GetComponent<CharacterController>().enabled = true;
Your answer
![](https://koobas.hobune.stream/wayback/20220612194551im_/https://answers.unity.com/themes/thub/images/avi.jpg)