- Home /
live system for sword
Hi, I used this script on player character
void OnControllerColliderHit(ControllerColliderHit hit)
{
if(hit.collider.tag=="sword_enemy")
{
col="hit";
live--;
}
}
but after collide , live shows -189 or more, this result is for one collide not more, how can i fixed it? one collide and one live--
i tried by OnTriggerEnter,too and that result was wrong
this is my complete code
using UnityEngine; using System.Collections;
public class playerCollid : MonoBehaviour { public string col=""; public int live=5; // Use this for initialization void Start () {
}
// Update is called once per frame
void Update () {
}
void OnControllerColliderHit(ControllerColliderHit hit)
{
if(hit.collider.tag=="sword_enemy")
{
col="hit";
live--;
}
}
void OnGUI()
{
GUI.Label(new Rect(2,2,100,100),live.ToString());
}
/*
void OnTriggerEnter(Collider other)
{
if(other.tag=="sword_enemy")
{
print("trigger");
}
}
*/
}
What was the result with OnCollisionEnter() ? Because I would think that is a better event to handle here.
I used the character controller for the player so I think i've to use the OnControllerColliderHit(ControllerColliderHit hit) , but i tried with OnCollisionEnter() too, but its didnt show any collide with this method, i dont no why? by the way thanks so much DaveA
I'm guessing that OnControllerColliderHit() is getting called (much) more than once per frame. If using OnCollisionEnter, did you set the .isTrigger = true on the character controller?
I added .isTrigger=true on the character controller, but doesn't work yet, I think the OnCollisionEnter just work on rigidbody , I added a cube as a sword and attached the script on it(for test), but doesnt work when character touch it, thanks DaveA
OnCollisionEnter is the best method for me but it works just between two rigidbody collide to another(i tested it), but how can I use OnCollisionEnter for character controller and a cube?( i can not use rigidbody for character or for cube)
Answer by Myth · Mar 21, 2011 at 09:36 PM
Test for a hit as you have,
then test if it is the first hit,
if so apply damage,
if not ignore,
if no hit then reset the var you use to check if it is the first hit.
good luck with whatever method you end up using!
Your answer
![](https://koobas.hobune.stream/wayback/20220613114029im_/https://answers.unity.com/themes/thub/images/avi.jpg)