x


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");
        }

    }
    */
}
more ▼

asked Mar 21 '11 at 06:38 PM

tahamtan gravatar image

tahamtan
3 2 4 7

What was the result with OnCollisionEnter() ? Because I would think that is a better event to handle here.

Mar 21 '11 at 07:08 PM DaveA

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

Mar 21 '11 at 08:33 PM tahamtan

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?

Mar 21 '11 at 09:51 PM DaveA

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

Mar 21 '11 at 10:37 PM tahamtan

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)

Mar 21 '11 at 10:44 PM tahamtan
(comments are locked)
10|3000 characters needed characters left

1 answer: sort newest

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!

more ▼

answered Mar 21 '11 at 09:36 PM

Myth gravatar image

Myth
404 21 27 42

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x804
x274
x141
x15

asked: Mar 21 '11 at 06:38 PM

Seen: 907 times

Last Updated: Mar 21 '11 at 07:06 PM