- Home /
Cannot move FPSController after respawning at a spawn point.
I have looked up a guide on how to respawn a player after they collide with an object (water/outside of terrain), and it works perfectly. But I am trying to attempt to do the same when their HP is reduced to 0. (In context, the scene loads at one point, and the respawn point is in a small building) Placing an invisible 3D box and disabling mesh for the colliding part works fine and all, but when the player is reduced to 0 health, he gets stuck in that invisible and not colliding object. I am able to rotate, attack and do everything else, but the player itself cannot move.
Here's the code for the water/terrain collider
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RespawnScript : MonoBehaviour
{
[SerializeField] private Transform player;
[SerializeField] private Transform respawnPoint;
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Player"))
{
player.transform.position = respawnPoint.transform.position;
Physics.SyncTransforms();
}
}
}
And this is the code for the Player's HP and its respawn point
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerHP : MonoBehaviour
{
[SerializeField] private Transform player;
[SerializeField] private Transform respawnPoint;
public int maxHealth = 100;
public int currentHealth;
public HealthBar healthBar;
void Start()
{
currentHealth = maxHealth;
healthBar.SetMaxHealth(maxHealth);
}
void Update()
{
if (Input.GetKeyDown(KeyCode.T))
{
TakeDamage(20);
}
if (currentHealth <= 0)
{
player.transform.position = respawnPoint.transform.position;
Physics.SyncTransforms();
}
}
void TakeDamage(int damage)
{
currentHealth -= damage;
healthBar.SetHealth(currentHealth);
}
}
Thanks in advance
Helo.
It can be so many things, its hard to say without testing it, only you can find where is the problem.
You should debug your code while running (look for tutorials), and see at what point the code does not do what you expect.
then investigate about that specyfic thing
Answer by Namey5 · Mar 07, 2020 at 11:51 PM
You respawn the player when their health reaches zero, but you don't actually reset the player's health. Therefore, the player is being respawned every frame, meaning you can still move, but the next frame you will be moved back to the spawn point. Instead, try the following;
if (currentHealth <= 0)
{
player.transform.position = respawnPoint.transform.position;
Physics.SyncTransforms();
currentHealth = maxHealth;
}
Then it's clear :D!
But, you must learnt o debbug your code. You will have 10000 situations like this while progam$$anonymous$$g, and must learn how to detect the "wrong" situation. Learn to Debug!
Erm, this line of code did fix the fact that my character gets stuck, but the HP slider does not revert back to its 100% state, the slider is completely empty and only changed when I receive damage, it gets down to 80% and then shows the Green color which is also meant to happen when the HP is on 100%. This does seem like a very easy thing to fix, but I am not that experienced with code, and I'm still learning the steps.
When you set the current health to max health in this answer you need to also update the health bar like you do inside the TakeDamage function.
I get what you are saying, but may I know how will I implement this into my code and what exactly should I write for this?
Your answer
Follow this Question
Related Questions
OnCollisionEnter 1 Answer
Object going through another....How to solve it? 0 Answers
How to decrease the speed of the player by some fraction after triggering the box collider? 2 Answers
Collision detection and prevention between a rigid body and box collider 0 Answers
How do you make collider interactions and movement smooth? 0 Answers