Search Unity

Katamari Experiment

Discussion in 'Scripting' started by Dranore, Jun 14, 2007.

  1. Dranore

    Dranore

    Joined:
    May 23, 2007
    Posts:
    57
    So I decided to do something as my first non-tutorial learning experiment. I figured I'd play around with making a Katamari Damacy style sticky ball. I've tried straight parenting the 'rolled' object and I've tried the sticky grenade script. The issue I'm having is that the ball has moved past the object by the time it's actually parented making the mesh float in orbit around the ball as opposed to on the surface. Any thoughts on how I might attach the child more accurately while maintaining collisions on the children?
     
  2. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    I guess Jonathan should have some tips; he's done a katamari-style experiment in the past...
     
  3. Jonathan Czeck

    Jonathan Czeck

    Joined:
    Mar 17, 2005
    Posts:
    1,713
    Here is a snippet that really is the bulk of the code. It won't work by itself but should give an idea on how I made things attach to the player. In short, you don't need to use any sort of joint. Just get rid of the object's rigidbody and then use transform parenting.

    Code (csharp):
    1.     void OnCollisionEnter(Collision info)
    2.     {
    3.         if (info.gameObject.tag != "Pickupable")
    4.             return;
    5.        
    6.         bool hitMainCollider = false;
    7.         // We only want things to attach if they hit the main sphere
    8.         foreach (ContactPoint contact in info.contacts)
    9.         {
    10.             if (contact.thisCollider == collider)
    11.             {
    12.                 hitMainCollider = true;
    13.                 break;
    14.             }
    15.         }
    16.        
    17.         // Here is the main Unity magic
    18.         if (hitMainCollider)
    19.         {
    20.             // If there is no rigidbody it should be a big flagarant error so we catch it, so don't check.
    21.             if (info.rigidbody.mass < rigidbody.mass * pickupableRatio  info.rigidbody.isKinematic)
    22.             {
    23.                 rigidbody.mass += info.rigidbody.mass;
    24.                 Destroy(info.rigidbody);
    25.                 info.transform.parent = transform;
    26.             }
    27.         }
    28.     }
    29.  
    Cheers,
    -Jon
     
  4. Dranore

    Dranore

    Joined:
    May 23, 2007
    Posts:
    57
    Thanks for the info Jonathan. I'll definitely play with it. I tried out your version. It's missing two things: the softening of the outside object - which would strike me as incredibly difficult to do - where the objects that stick out too far slowly get "averaged" to make it easier to roll with them over time... so when you hit that telephone pole you aren't screwed. Also there should probably be some sort of larger sphere around the ball to make picking up objects a bit easier. As soon as you've collected a few objects, getting more to attach becomes very difficult. Skill in picking up objects isn't a very fun game. hehehe... So there's probably some way of having an invisible sphere around the sphere that gives you a little extra room to attach things that are *close* but can quite stick because of ojbects you've already attached. Really interesting work though. I enjoyed it.