- Home /
unity sometime execute instruction and sometime ignore same instruction ?!
Greetings
I have script which respawn player if he fall into water ( water object is plane ), this water object send damage to player health then player die and respawn to spawn point. But sometime player does not respawn and he is keep falling then i need to restart the game.
these are the scripts
Player health:
public int maxHealth = 100;
public Transform spawnPoint;
public int health = 100;
private bool isDead;
void Start()
{
health = maxHealth;
}
void Update()
{
if(isDead)
{
Respawn();
}
}
public void TakeDamage(int amount)
{
health -= amount;
if (health <= 0)
Die();
}
void Die()
{
isDead = true;
}
void Respawn()
{
transform.position = spawnPoint.position;
health = maxHealth;
isDead = false;
}
Water:
public int damage = 100;
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Player"))
{
other.GetComponent<PlayerHealth>().TakeDamage(damage);
}
}
what is the problem ?!
Answer by talyh · Jun 15, 2019 at 04:29 AM
Planes are paper thin, so it seems the collision is not being detected on some cases. That’s a common issue. You’ll likely want to make your water have a collision box with some height to allow a couple of frames of contact between player and water to ensure the collision is identified.
Also, as a fallback, you may have some checks on the player’s vertical position. If it’s less than whatever makes sense, the player fell out of the world and should be killed.
Thank you for your replay
i have detected where is the problem.
the problem in Update function when i 've changed it to FixedUpdate function, it detect the collisions (Trigger) better than Update function. and to make sure water detect player, i've changed mesh collider to Box collider. Plus, if player falls into water repeatedly, he may can pass the collider (Trigger) and keep fall down again and go back to same problem.
Answer by Bunny83 · Jun 14, 2019 at 09:32 PM
What kind of collider does your water "plane" object use? You should use a box collider with sufficient thickness. If the player moves at a high velocity it's possible that the player misses a plane mesh collider and just falls "through". At the right speed there will be no overlap between the colliders.
Have you actually checked with a Debug.Log if you enter the OnTriggerEnter method when your player falls through the trigger? If it doesn't fire, there will be no overlap. Of course all this assumes that you player actually has a rigidbody attached.
Thank you for your replay
i have detected where is the problem.
the problem in Update function when i 've changed it to FixedUpdate function, it detect the collisions (Trigger) better than Update function. and to make sure water detect player, i've changed mesh collider to Box collider. Plus, if player falls into water repeatedly, he may can pass the collider (Trigger) and keep fall down again and go back to same problem.
Your answer
Follow this Question
Related Questions
RigidBody.velocity or transform.position 1 Answer
HELP! Scripts looks fine but doesn't respond? 1 Answer
How do I use UNetWeaver.dll during runtime? 0 Answers
How to count from 2nd collision 1 Answer