Search Unity

Collision between a sword and an object

Discussion in 'Scripting' started by moynzy, Mar 26, 2015.

  1. moynzy

    moynzy

    Joined:
    Oct 22, 2014
    Posts:
    82
    Code (CSharp):
    1.     void OnCollisioEnter(Collision collision){
    2.         Debug.Log ("COLLISDED");
    3.  
    4.         if (collision.gameObject.name == "sword") {
    5.             DestroyMe();
    6.         }
    7.    
    8.     }
    9.  
    10.     void DestroyMe(){
    11.         if(debre)
    12.             Instantiate (debre, transform.position, transform.rotation);
    13.        
    14.         Destroy (gameObject);
    15.  
    16.     }
    my sword has a box collider on it, and it is tagged "sword".

    So when the swords box collider collides with object box collider nothing happens?

    Anyone know a good resolve for a simple collision between the two, maths is one, but in terms of components and scripting.

    I can provide graphical information if needed.
     
  2. psyydack

    psyydack

    Joined:
    Aug 30, 2013
    Posts:
    93
    Your script is wrong... it have a N less.

    Change this : void OnCollisioEnter(Collision collision){
    To this : void OnCollisionEnter(Collision collision){


    This my test :
    Code (CSharp):
    1. void OnCollisionEnter(Collision col)
    2.     {
    3.         print (col.transform.name);
    4.         col.transform.GetComponent<MeshRenderer>().material.color = Color.red;
    5.     }
    Maybe you need to put rigidbody at leat in one.
     
    moynzy likes this.
  3. moynzy

    moynzy

    Joined:
    Oct 22, 2014
    Posts:
    82
    That's really embarrassing. Thank you, let me check your test. One of the components does have a rigid body.
     
  4. moynzy

    moynzy

    Joined:
    Oct 22, 2014
    Posts:
    82
    ]

    interesting.

    Code (CSharp):
    1.     void OnCollisionEnter(Collision col)
    2.     {
    3.         if (col.transform.name == "Terrain") {
    4.  
    5.         } else {
    6.             print (col.transform.name);
    7.  
    8.             col.transform.GetComponent<MeshRenderer>().material.color = Color.red;
    9.  
    10.         }
    11.      }

    It sees the Terrain as a collision, but my character and sword have both box components. The object that I want the sword to collide with has a rigidbody, and box collider.

    Yet, no collision, only collision between terrain and object.
     
  5. Fliperamma

    Fliperamma

    Joined:
    Apr 24, 2012
    Posts:
    31
    If your object has a tag then you must compare the GO's tag and not the GO's name ... also you can try to use OnTriggerEnter instead.
    Like this
    Code (CSharp):
    1.  
    2. void OnTriggerEnter(Collider other) {
    3. if (other.CompareTag("sword"))
    4. // Do your stuff
    5. }
    6. }
    7.  
    Additionally, take a look at this comparison table (at the end of the page) http://docs.unity3d.com/Manual/CollidersOverview.html and check if your rigidbody / collider / trigger configurations are correct.
     
  6. moynzy

    moynzy

    Joined:
    Oct 22, 2014
    Posts:
    82
    This is good resolve, I only used OnTriggerEnter for my EnemyAI, never thought of applying it to collision.
    Thank you for this. I will compare the colliders.
     
    Fliperamma likes this.