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

Platformer Climb-Up-Ledge animation issues

Discussion in 'Scripting' started by SorneBachse, Feb 8, 2015.

  1. SorneBachse

    SorneBachse

    Joined:
    Dec 27, 2012
    Posts:
    62
    Hey peeps.

    I have a problem with a platformer game I am working on. It happens specifically when my character is hanging on a ledge and then pressed the 'up' key, to get onto the platform. What I am doing is following:

    -When the player is hanging on a ledge and presses up, I start the "Crawl-Up-Ledge" animation, which only moves the child object with the animator, and not the parent GameObject.


    Then when the animation is complete, I start a Coroutine which then moves the parent gameobject to the desired position, and sometimes that is totally fine.

    The Coroutine code looks like this:
    Code (CSharp):
    1. IEnumerator MoveTransformCoroutine()
    2.     {
    3.         yield return new WaitForSeconds(0.423f);
    4.         rb.Sleep();
    5.         collider.enabled = false;
    6.         Vector3 thePos = playerCollState.LedgeEndPos;
    7.         rb.MovePosition(thePos);
    8.  
    9.         climbUpLedge = false;
    10.         rb.velocity = Vector3.zero;
    11.         rb.useGravity = true;
    12.         collider.enabled = true;
    13.         playerCollState.IsLedgeHanging = false;
    14.         rb.WakeUp();
    15.     }
    The WaitForSeconds parameter is the amount of time the crawl-up-animation is. (approximately).

    But at completely random times, this happens:

    The parent gameobject is moved before the animation is complete. Or so it seems that way atleast.

    It looks like it happens in a single frame, because the child object will very shortly thereafter return to the original position.

    The crawl-up-animation will then transition to the idle state, with a transition time of zero.

    I have no idea why it is not either consistently creating this bug, or not at all.
    Do you guys have any idea what the problem might be? Or should I try to approach this in a completely different way?

    Thanks in advance!
     
  2. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    897
  3. SorneBachse

    SorneBachse

    Joined:
    Dec 27, 2012
    Posts:
    62
    I didn't even know you could do that. That is a brilliant idea! Will try tomorrow and report back with the results :)

    Thanks a bunch!
     
  4. SorneBachse

    SorneBachse

    Joined:
    Dec 27, 2012
    Posts:
    62
    I tried to trigger the function when the animation was completed, but it still randomly seems to happen. I've also tried to make the MoveTransform() into a regular function, and then tried to call it from LateUpdate. But no luck either.
     
  5. SorneBachse

    SorneBachse

    Joined:
    Dec 27, 2012
    Posts:
    62
    I've also tried to make a Coroutine which will yield return null, while the climbUp animation is running, but it just completely ignores the while loop.

    Anyone else have any other ideas/solutions?
     
  6. BrendanKZN

    BrendanKZN

    Joined:
    Jun 22, 2011
    Posts:
    157
    Hey, I did a similar thing a while back but with a 3D model, I will dig up the code / method tonight (in about 2hrs) and see how I went about doing this, hopefully helping you out unless someone else beats me to it.
     
  7. SorneBachse

    SorneBachse

    Joined:
    Dec 27, 2012
    Posts:
    62
    Thank you, kind sir!
     
  8. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    897
    Still think it should be able to do this with functions called from the animation. However, shouldn't your coroute look like this (otherwise it waits a bit and then do everything at once, both disabling and enabling at the same time)

    Code (CSharp):
    1. IEnumerator MoveTransformCoroutine()
    2.     {
    3.         rb.Sleep();
    4.         collider.enabled = false;
    5.         Vector3 thePos = playerCollState.LedgeEndPos;
    6.         rb.MovePosition(thePos);
    7.  
    8.         yield return new WaitForSeconds(0.423f);
    9.  
    10.         climbUpLedge = false;
    11.         rb.velocity = Vector3.zero;
    12.         rb.useGravity = true;
    13.         collider.enabled = true;
    14.         playerCollState.IsLedgeHanging = false;
    15.         rb.WakeUp();
    16.     }
     
  9. SorneBachse

    SorneBachse

    Joined:
    Dec 27, 2012
    Posts:
    62
    You are right. Fixed that. Thank you. Although it did not fix my bug. Still keeps happening.
     
  10. BrendanKZN

    BrendanKZN

    Joined:
    Jun 22, 2011
    Posts:
    157
    Hey, I have had a look and 2 separate occasions I've had to do similar to what you are trying. I think I even posted here a few years ago asking a similar thing. Haha. The randomness of you getting the issue though is another concern but it could just be the way you are going about it.
    The best method that has worked for me, It may not make sense but it actually works pretty well, is as follows:
    Example I want a player to hop up one stair, I first use transform.Translate to the exact position I want the player at the end of the animation, then I play the animation straight after the translate line of code, BUT you need to do the animation from the other way around, so instead of animating from the origin to your desired position, you animate from below to the origin. I really hope this makes sense. It all comes down to the animation tho, you have to do get your positions spot on...
     
    chelnok likes this.
  11. SorneBachse

    SorneBachse

    Joined:
    Dec 27, 2012
    Posts:
    62
    Huh.. That's actually pretty clever and might be a better solution. I'm gonna have to try that. That does mean I need to create a new animation and do a little work-around in the code, unfortunately. But if that is what it takes!

    Thanks a lot :) Will report back in a couple of days with the results.