Forum rules - please read before posting.

2D animation doesn't stop on collision

Hi to everybody.

I'd like to make my character to stop its animation after a collision, but I can't get it.

I'm working with this parameters, in a 2D project:
  • Movement method: Direct (to be able to use the keyboard and to avoid the mouse)
  • Input method: Keyboard Or Controller

As the NavMesh is not working in Direct mode, I've created some Collision Cubes 2D (from the Scene tab) to draw the limits of the scenary. Besides, for the character I've created a Rigidbody 2D (freezing positions has not effect) and a Circle Collider 2D, and made the Player section to Ignore gravity (or it goes down).

And it works: my character doesn't cross the colliders. The problem is, as I've said, that it's moving all the time as it was walking in the same direction. I'd like to put him in an idle state just after the collision.

Thank you very much if someone can help!

(By the way... I'm a beginner in Unity issues. Sorry :( )

Comments

  • This feature is currently only available for 3D characters - however, I will see if it's possible to add for 2D characters as well.
  • Thank you. I hope this feature or a "manual" code solution is possible soon. :)
  • Hi again.

    I've found a "craft" way to solve part of the problem. When the distance between the position in a Update event and next is lower than some number, the animator component speed is 0. Otherwise, animator component speed is set to a default value. See the code below:

    #pragma strict

    public var MovementThreshold : float = 1;
    public var WalkingSpeed: float = 1;
    private var animator: Animator;
    private var lastPos: Vector3;

    function Start () {
        animator = this.gameObject.transform.GetChild(0).GetComponent.<Animator>();
        lastPos = new Vector3(0, 0, -10);
    }

    function Update () {
        if (Vector3.Distance(this.transform.position, lastPos) * 1000 < MovementThreshold)
            animator.speed = 0;
        else if (animator.speed == 0)
            animator.speed = WalkingSpeed;
        lastPos = this.transform.position;
    }

    It's perfect with the default values. My problem now is that I'm not able to change the animator to "Idle" (for example, "BrainIdle"), so, the character remains in an unnatural position. I've tried with animator.Play, but it can't get it.

    Any help?

    Thanks!

  • Have you looked at the Sprites Unity Complex animation engine?  Check the manual's section 3.9.  The default Sprites mode, "Sprites Unity", handles the playing of Mecanim animations automatically - making it easy to set up, but restrictive in terms of scope.

    Sprites Unity Complex, however, allows you to play your animations using regular Mecanim parameters, e.g. "movement speed", "facing direction" etc.  AC will control the parameters and nothing else - therefore, you can insert your own parameters, and control them through script.  If you're going down the custom script route, that'd be your best bet.
  • An update: this will be a feature in v1.47 - but only with Sprites Unity Complex characters.  Character movement speeds will be able to be reduced as they need "wall" colliders - if you reduce it by a big enough amount, you'll be able to stop them completely.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.