Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

C# Ragdoll Material Change On Collision?

Discussion in 'Scripting' started by aranaren, Feb 18, 2015.

  1. aranaren

    aranaren

    Joined:
    Feb 18, 2015
    Posts:
    2
    Hello, I'm working in C# with a ragdoll, trying to get the color/material to change on individual pieces (eg RightCalf) when they collide with anything other than the connected bones of the ragdoll.

    At the moment, my changeonimpact.cs script works fine if I attach it to environment items like cubes (will swap to Color.red / Color.blue for itself and the nearby cubes in a stack) but it never changes anything on a ragdoll (even though I can get the log to print collision.contacts[0].thisCollider.name and it lists bones, so I think it's hitting fine?)

    Each bodypart is a child of a character (not player) and has its own rigidbody and collider (box or cylinder). I feel like there must be an easy way to do this, but I'm stuck - love Unity for 2D but really struggling with a limited understanding of 3D :(

    Ideally I wanted to use Mathf.PingPong and material.Lerp to transition between the two materials (so that it would fade in/out instead of flash to a new one) but at this point anything that may work would be massively helpful and appreciated.
     
    Last edited: Feb 18, 2015
  2. CodeMonke234

    CodeMonke234

    Joined:
    Oct 13, 2010
    Posts:
    181
    Does each bodypart also have separate mesh?

    Often just a single mesh is mapped to the skeleton.

    If it does have separate mesh, make sure to get the correct gameObject with the renderer on it...it may be a child?

    use GetComponentsInChildren
     
  3. aranaren

    aranaren

    Joined:
    Feb 18, 2015
    Posts:
    2
    Hi CodeMonke, thanks for the reply!
    The ragdoll is a collection of meshes rigged with a skeleton. All body parts are children. (eg Pelvis -> LeftThigh -> LeftCalf)
    Originally only the main object had a renderer, but I tried adding one to all children (body parts).. in either case it still won't work. I did also try targeting individual parts directly (though this seems inefficient?), for example:
    Code (CSharp):
    1. void OnCollisionEnter (Collision collision)
    2.     {
    3.         print("My name is: " + collision.contacts[0].thisCollider.name);
    4.         if(collision.gameObject.name == "Pelvis")
    5.         {
    6.             collision.gameObject.renderer.material.color = Color.blue;
    7.         }
    8.     }
    Is that what you meant? Despite knowing for sure (via log) that the pelvis (and other parts down the lines in code) hit a cube, no material changes. Did a similar code with the GetComponentsInChildren but couldn't get it to work. I've tried so many versions at this point.. not sure what to do :(

    Could you (or anyone) give me an example using GetComponentsInChildren? Maybe I made a mistake with mine.
     
    Last edited: Feb 20, 2015