- Home /
Not identifying the collider?
Basically, in reference to my healthbar (which works perfectly fine), I have my enemy running up to me and attacking when (distance<1), however it's not detecting any colliders at all, they all have character controller/rigidbody etc. It just doesn't remove health upon impact. How do I do this?
This is my code.
@script ExecuteInEditMode()
var healthTexture : Texture2D;
var healthBorder : Texture2D;
enemy = GameObject.Find("Enemy");
player = GameObject.Find("Player");
var health : int = 100;
var adjust : int = 0;
function OnCollision(other:Collision)
{
Debug.Log("Collided with " + other.gameObject.name);
if (other.gameObject.tag == "Enemy")
{
other.gameObject.tag == "Player";
health -= 10;
}
}
function OnGUI () {
GUI.DrawTexture(Rect(0,Screen.height - 600,290,100), healthBorder);
//Now on top we can put Health Bar texture
adjust = health * 1.5; //adjusting texture size (width) / health(100)
GUI.BeginGroup(Rect(122,Screen.height - 565,adjust,15));
GUI.DrawTexture(Rect(0,0,290,15), healthTexture);
GUI.EndGroup();
}
Look forward to your answers.
Answer by JinxM · Apr 16, 2012 at 02:14 PM
Likely the error is that you created a custom function OnCollision() instead of using the event OnCollisionEnter().
Also your if statement looks a bit strange to me. The other.gameObject.tag == "Player" line doesn't do anything.
Is this script placed on your Player object? If so, then I'd expect to see something more like this:
function OnCollisionEnter (other : Collision) {
if (other.gameObject.CompareTag ("Enemy")) {
health -= 10;
}
}
Sorry, I had my code like this before I changed it, it was the same scenario. 0 change :/
Answer by aldonaletto · Apr 16, 2012 at 02:15 PM
Collisions may be tricky: OnCollision events are sent to the scripts attached to both objects when the rigidbody hits the CharacterController, but not when the character hits the rigidbody. To detect the character hitting something, you must use OnControllerColliderHit in the character script (nothing is sent to the other object).
In order to apply damage to the other object when a character hits it, you must use something like this (character script):
function OnControllerColliderHit(hit: ControllerColliderHit){ if (hit.gameObject.tag == "Enemy"){ // call the function ApplyDamage in the enemy script: hit.gameObject.SendMessage("ApplyDamage",10); } }And you must add the ApplyDamage function to the enemy script:
function ApplyDamage(damage: float){ health -= damage; if (health The same applies to the enemy-hitting-player case.
This doesn't seem to work, it's not identifying the collider AT all.
Both answers are right - $$anonymous$$e and @Jinx$$anonymous$$'s - one of them should work, depending on some details. What's the player? A character controller or a rigidbody? And what about the enemy?
Both of them are character controllers.
I've even removed the character controller from the enemy and replaced it with a rigidbody.
I've even tried
function OnControllerColliderHit(hit: ControllerColliderHit){
print("Does this work yet?");
}
But it doesn't print out anything. :/
UPDATE - Nvm, I got it working now. Just need to figure out how to make my enemy stop 2m from the player ¬_¬
almost sounds like one of the charaters his "players"is not tagged player. what was the problem if you don't $$anonymous$$d sharing. curosity so that if i run into this problem i know where to look.
The sword itself didn't have a collider, so I set a box collider on the sword and did a print out. Everyone has a tag, as when I played the animation, the mesh collider on the sword was duplicated and pushed to another area. (Spartan$$anonymous$$ing model from the asset store, load it and you'll see what I mean).
That was about it.
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Fighting combo attacks 1 Answer
Melee Damage script by collision 2 Answers
Animations Combo 0 Answers