0

I have an object (a simple card) equipped with the Rigidbody, Collider and Animator components. In particular, Animator performs two simple clips: Cover and Uncover that rotates the card 180 degrees. The first starting from 0 degrees to 180, the second from 180 to 0.

So the clips should cover or uncover the card. The problem is that the card returns to its original state.

For example, the original state of the card is the "uncovered state" so when I click on it to cover it covers as expected. The I register the state "covered" programmatically. However, immediately after the animation ends, it returns to its original state (uncovered). The card now has an internal (C# variable) state as "covered", but I see it to still be uncovered. When I click it again, will be fired the correct animation clip: "uncover".

What should I do to leave it covered at the first click?

I have uploaded here a short clip that shows the behavior of the card, also below find some screenshots that refer to the various views of the IDE that may have useful information. The video clip is uploaded to Dropbox for the while.

The video of the behavior: Dropbox link

The code of the Flip script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Flip : MonoBehaviour {

    private Animator anim;
    private enum cs_e {covered, uncovered};
    private cs_e coveringState;

    // Use this for initialization
    void Start () {
        anim = this.GetComponent<Animator>();
        coveringState = cs_e.uncovered;
    }

    // Update is called once per frame
    void Update () {

    }

    private void OnMouseUp()
    {
        Debug.Log("Mouse up");


        if (coveringState == cs_e.uncovered)
        {
            anim.Play("Cover");
            coveringState = cs_e.covered;
        } else {
            anim.Play("Uncover");
            coveringState = cs_e.uncovered;
        } 

    }

    private void OnMouseDown()
    {
        Debug.Log("Mouse down");
    }

    private void MouseDrag()
    {
        Debug.Log("Mouse drag");
    }
}

Some screenshots:

enter image description here

enter image description here

enter image description here

enter image description here

4
  • 1
    Did you put conditionals on your state transitions on your animator? Also I don't think you need to walk arround iddle, if you are covered, u can be uncovered, and this is bidirectional, no need to walk through idle i think. Btw: to put conditionals on your animator, click on the "arrow" which is your transition, and set it there.
    – Lotan
    Jun 4, 2018 at 10:14
  • I saw the conditional only now. I would not steal precious time from you. I can try to follow a tutorial, I think I'll look for one. If in the meantime you could think of a tutorial that could be useful in my case, please let me know here. Evidently I'm not yet very familiar with the animations, I'll have to study more. Thank you!
    – shogitai
    Jun 4, 2018 at 10:24
  • 1
    If you don't find any easy tutorial, let me know and I'll try to make a "complete and simple" answer to your problem here (but I prefer to give you some time, the reward is better if you can find your own solution! :D)
    – Lotan
    Jun 4, 2018 at 10:35
  • 1
    I did it, the problem was the states of the Animator Controller (I removed the transitions) and I turned off the animation looping. Also, I partly redid some things and probably can not remember the previous configuration (when I had the problem). In any case, doing things well again, now everything works. Now different problems have arisen (identifying mouse dragging and differentiating it from clicks), but this is another chapter of my long Book of Problems.
    – shogitai
    Jun 4, 2018 at 11:00

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.