Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by DenissK · Mar 07, 2020 at 09:55 PM · collisionmovementcolliderrespawn

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

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image tormentoarmagedoom · Mar 07, 2020 at 10:12 PM 0
Share

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

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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;
 }

Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image tormentoarmagedoom · Mar 08, 2020 at 11:53 AM 0
Share

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!

avatar image DenissK · Mar 08, 2020 at 05:14 PM 0
Share

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. alt text

alt text

avatar image joemane22 DenissK · Mar 08, 2020 at 07:26 PM 0
Share

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.

avatar image DenissK joemane22 · Mar 08, 2020 at 08:21 PM 0
Share

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?

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

251 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges