- Home /
How do I make a character die when he fells.
What I'm trying to do is whenever the player falls from the main ground and enters hellzone which is placed underneath main ground a function is called."hellzone" here is a simple plane with isTrigger and isKinematic on , it also has a tag applied to it saying "hell". On the other hand my player has a rigidbody with isKinematic off and a mesh collider with isTrigger off.
The problem here is that when I enter the "hellzone" nothing happens, my player keeps falling after it slips through "hellzone".
Here is the script that manipulates all this `
// death from falling
function OnTriggerEnter(other: Collider){
if(other.gameObject.tag=="hell"){
StartCoroutine(Respawn());
Debug.Log("Collided");
}
}
// Respawn
function Respawn(){
GameManager.lives--;
renderer.gameObject.active = false;
yield WaitForSeconds(respawnTime);
transform.position = respawnPoint.position;
renderer.gameObject.active = true;
}
Also, here is the entire player script just in case you need to look through that
var speed:float;
var gravity_:float;
var jumpSpeed:float;
private var grounded = true;
var respawnTime:float = 5.0;
var respawnPoint:Transform;
function Start () {
}
function FixedUpdate () {
// movement
var amtToMove:float = speed*Input.GetAxis("Horizontal")*Time.deltaTime;
transform.Translate(amtToMove,0,0,Space.World);
// Jump implementation
if(Input.GetKeyDown("space")&&grounded){
rigidbody.velocity = Vector3(0,jumpSpeed*Time.deltaTime,0);
}
// gravity implementation
rigidbody.AddForce(Vector3(0,-gravity_*rigidbody.mass*Time.deltaTime,0));
grounded=false;
}
function OnCollisionStay(other:Collision){
// ground check
if(other.gameObject.tag=="ground"){
grounded = true;
}
}
// death from falling
function OnTriggerEnter(other: Collider){
if(other.gameObject.tag=="hell"){
StartCoroutine(Respawn());
Debug.Log("Collided");
}
}
// Respawn
function Respawn(){
GameManager.lives--;
renderer.gameObject.active = false;
yield WaitForSeconds(respawnTime);
transform.position = respawnPoint.position;
renderer.gameObject.active = true;
}
Answer by whydoidoit · Aug 26, 2012 at 11:17 AM
It's possible you character is moving too quickly and passes through the collider. You could try making the collider much taller and offsetting it or you could ensure that the collision mode is Continuous Dynamic on your player which should make the collision easier to detect.
MeshColliders are a bit tricky sometimes (subject to back face culling for instance) but I would have thought that it would be ok in this circumstance. You are often better off approximating a collision area for you player using a combination of primitives.
Setting the collision mode to Continuous Dynamic didn't help but switching to box collider and then increasing it's size sure did, however there seems to be a problem, my position doesn't change to respawn point's position.
EDIT:since the lives decrease I'll assume that the Coroutine is being called but the "renderer.gameObject.active = false" doesn't work, what should I do?
Yes that will be because when you make the game object inactive the coroutine will not be called again.
Just do:
renderer.enabled = false;
...
renderer.enabled = true;
Hmm.. now yield WaitForSeconds(respawnTime); transform.position = respawnPoint.position; works but I've a question ,why doesn't doing renderer.enabled = false; make my character invisible?
Nevertheless, you solved my problem with the exception of being invisible so I'll accept your answer as correct. Thanks a lot sir.
Was that not what you wanted by making the game object inactive? Sorry maybe I've lost what you were trying to do there.
renderer.enabled will make your character invisible until the respawntime has passed.
Your answer
Follow this Question
Related Questions
Trigger respawn after collision? 3 Answers
Trigger Respawn 1 Answer
If trigger hit, spawn it 1 Answer
Can't add Rigidbody component because collision prevents it 2 Answers
how to make triger fireball not to pass through spesific objects 2 Answers