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

RigidBody Woes; other ramblings

Discussion in 'Scripting' started by RJ-MacReady, May 23, 2014.

  1. RJ-MacReady

    RJ-MacReady

    Joined:
    Jun 14, 2013
    Posts:
    1,718
    Rigidbodies are an awesome way to easily get the benefits of a physics engine in a game... as long as you're coding a billiards simulation. Anything fast or precise and it just doesn't work. I find myself banging my head against the keyboard, just now realizing that I'll need to write my own "physics" scripting for a simple 2.5d sidescroller after a week of trying to integrate standard asset physics. On the bright side, it will still work for blowing up cars and trash cans. :cool:

    Also, what's with this cacophony?

    someObject.GetComponent<SomeScript>().SubObject.SomeMethod(); // Terrible readability, but really useful

    ...anyway, just learning Unity I guess. My head hurts.
     
  2. SiegfriedCroes

    SiegfriedCroes

    Joined:
    Oct 19, 2013
    Posts:
    569
    There's nothing wrong with the RigidBody component in my opinion... What are you trying to do that is not working?
     
  3. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    Unity Physics is useful in small cases. I find mainly detecting collisions and triggers, and stuff you can fire and forget. Eg: blowing up a car or trash can and it doesn't really matter where it lands. Just that it looks cool.

    But yeah, a lot of the physics you will need to calculate yourself if you really want it to act exactly right. Eg: I track velocity and calculate gravity myself, then move the rigidbody myself.

    As for your giant long line of code there, if you're doing that all the time I would just hold a reference to the SubObject.
    Code (csharp):
    1.  
    2. void Start() {
    3.   subObject = someObject.GetComponent<SomeScript>().SubObject;
    4. }
    5.  
    6. public void RunSomeMethod() {
    7.   subObject.SomeMethod();
    8. }
    9.  
    10. SubObjectClass subObject { get; set; }
    11.  
     
  4. RJ-MacReady

    RJ-MacReady

    Joined:
    Jun 14, 2013
    Posts:
    1,718
    I was trying to use a RigidBody for my main character object. My purpose for doing so was to allow the built-in physics to handle pushing around objects in the environment, like crates and things. But also I wanted to be able to simulate "resistance" so as the player pushed through zombies and crates, they pushed back... causing slower movement and slight changes in direction. The main problem that I experienced is that Rigidbodies will not stop overlapping with one-another. They must collide and be partially inside of one-another to make their calculations, it seems. So unusual "sticky" effects occur.

    One great example is when beginning a jump standing near a wall, the jump will be slowed, stopped or otherwise interfered with by the physics engine because, according to rigidbody logic, near is still partially touching... if that makes sense. I experimented with materials and all different methods and approaches I could think of, but in the end I have abandoned the Rigidbody as a foolproof, reliable way of handling collision. It's as was Garth Smith said, for non-critical things. Not the main position of your character, which could become trapped in a wall or launched curiously sideways into a pit. For this I'm using a custom-defined raycast "cage" that surrounds my player collider (a cylinder). It's very reliable and I have no fear of unwanted collisions or the dreaded "stuck inside of another collider" scenario.

    I found yet another way around that script thing, exposing Scripts to the Inspector and then dragging the Gameobject containing scripts onto itself. Haha. Unity is quirky but fun.