I am learning character lives and collider from lesson 10-15 of http://www.youtube.com/user/TornadoTwins#p/c/9/cyXV3dUU30w
My game is that, there is a cylinder moving towards character. If cylinder hits character, character die.
static var gotHit = false;
function OnControllerColliderHit(hit : ControllerColliderHit)
{
if(hit.gameObject.tag == "enemyCylinder") //cylinder has that tag
{
gotHit = true;
PlayerLives.LIVES -= 1;
}
}
function LateUpdate ()
{
if(gotHit)
{
transform.position = Vector3(0,0,0);
gameObject.Find("Main Camera").transform.position = Vector3(0,0,0);
gotHit = false;
}
}
When the cylinder hits character, character lives deduct 1 and return to (0,0,0) position.
It works if character hits the center of cylinder. However, it does not work if character hits the left or right hand sides of cylinder.
Cylinder has rigidbody and capsule collider and Is Trigger is not checked.
So, what’s wrong with the code or settings?
Try to manipulate the size of the attached rigidbody of the cylinder or the character
/Chris
I did try to re-size the collider to be larger and found that it was not that problem.
I found it is the case:
-
If I tap the left joystick and collide with cylinder, character will die.
-
If I do not touch the joystick, just wait the cylinder collide with character, character will not die and both objects stand still.
So, how should I fix it?
The CharacterController checks for collisions that happen when it moves, but doesn’t report other incoming objects colliding with it. I know you were having trouble with the cylinder’s collision detection, but unfortunately, I don’t think you can solve them by using the OnControllerColliderHit function on the character.
Dear Sir
If I cannot use OnControllerColliderHit function, which function should I use or how is the proper way to fix it?
You probably just need an OnCollisionEnter function on the cylinder’s script. You can call the code to deduct the character’s life and reset his position from this function.
Thanks for help, it does work now.
Ahh this just helped me out a ton…Was exactly what I was looking for!