Search Unity

Best way to play canned animatations with collisions?

Discussion in 'Physics' started by Vidd, Jan 7, 2015.

  1. Vidd

    Vidd

    Joined:
    Jun 3, 2013
    Posts:
    7
    I've been puzzling over this since the weekend.

    Say I have a 2D character that can perform a set motion that's predefined as a number of points which it interpolates through. How do I let it move it through the path while at the same time being constrained by physical collisions? e.g. If it hops forward it doesn't go through walls. If it hits the ceiling, it stays or falls without going through.

    By my understanding if you use MovePosition, it'll clip through. Otherwise you have to use forces which won't work for complex paths.

    Is there a solution that doesn't involve breaking into a 3D editor and baking in root motion animations? Thanks in advance
     
  2. Nanako

    Nanako

    Joined:
    Sep 24, 2014
    Posts:
    1,047
    I don't think movePosition is supposed to clip through things. But if that is happening for you, then the best solution i think would be to add some collision handling to it. Depending on whatever you want to do when you hit a wall.

    ise the various collision monobehaviours to figure out when it hits something, then either make it stop moving, or move to a different target, or whatever.

    Also, try altering the collision behaviour of the object. Interpolate and extrapolate will most likely give different results

    And lastly, you can do just about anythiung with forces, imo. If you need a sudden change of direction then you just have to apply enough force to stop the object, in addition to enough force to move it in the desired direction. That requires some math
     
  3. Vidd

    Vidd

    Joined:
    Jun 3, 2013
    Posts:
    7
    I'd read a couple of times that MovePosition was only for kinematic bodies (that won't collide by design). I tested this after reading your post and while it mostly worked, it seems like it does clip a just a little then gets pushed out with some velocity. Not ideal but not terrible for these purposes.

    You're right that it certainly could be done with forces but the difficulty comes with trying to figure out how to achieve the complex motion rather than just predefining points. I might just end up going with this solution after all but it's far more fragile. If I change something like the scale, it'll need redone.

    Thanks very much for your post, it seems achievable now. :)
     
    Last edited: Jan 11, 2015
  4. Nanako

    Nanako

    Joined:
    Sep 24, 2014
    Posts:
    1,047
    The world is built on fragile things, though. for example in networking, packets are regularly dropped and lost to oblivion.

    The key is to build robust systems ontop of fragile foundations. A castle on quicksand, if you will. In networking this is generally done by checking if packets got there, and resending them if not.

    In your case, it should be done by checking whether the target (getting to a given position) has been achieved, or is BEING achieved. If an object is slowly moving sideways and not towards the target, then it's probably sliding against an obstacle.

    basically, just build error handling into your code. Expect that things will go wrong, figure out as many of those things as you can, and code in ways for your system to deal with them
     
    Vidd likes this.