playMaker

Author Topic: Unity3D: Returning back to idle state animation Problem  (Read 2313 times)

XxDivaxX1

  • Playmaker Newbie
  • *
  • Posts: 2
Unity3D: Returning back to idle state animation Problem
« on: July 19, 2016, 10:48:00 AM »
I'm having a problem with returning back to my idle state animation in my game. This video [https://youtu.be/TAM3hP-m3bo] is an example of what my problem is. My player doesn't go back to idle state animation, which I did try adding this to my code:

            anim.SetBool ("walking", false);

(This is placed near to the end of my code[http://pasteboard.co/cc1n2jEaG.png])

But this happens[https://youtu.be/PagWMwU7QGk]. Which is not what I want. In the first [https://youtu.be/TAM3hP-m3bo]video I showed you, it shows me clicking the walking parameter off[http://pasteboard.co/cbS0YMEtm.png] so that I can show you what my animation would look like if my player stop once it reached it's destination.In the video you can see that my player faced the wrong way after walking down and up, it faced this way [http://pasteboard.co/cbT1OhMaw.png]and that way[http://pasteboard.co/cbUgJcSpB.png], which I would also like to fix, but have no idea how to. So can anyone help me with both of my problems, which is:

1. Returning back to idle animation once my player reaches it's destination.
2. And making sure that my player face the correct direction as it is suppose to. My player's sprite image[http://i.imgur.com/Hz28tgl.png]


Here is my code:


    private Animator anim;
    public float speed = 15f;
    private Vector3 target;
    private bool touched;
    private bool playerMovementRef;

    void Start ()
    {
        target = transform.position;
        anim = GetComponent<Animator> ();
    }

    void Update ()
    {
        if (Input.GetMouseButtonDown (0))
           {
            Vector3 mousePosition = Input.mousePosition;
            mousePosition.z = 10; // distance from the camera
            target = Camera.main.ScreenToWorldPoint (mousePosition);
            target.z = transform.position.z;

            var movementDirection = (target - transform.position).normalized;
                Vector3 animDirection = Vector3.zero;
                // Use >= to default to horizontal on both being equal

            if (movementDirection.x > movementDirection.y)
                animDirection.x = 1;
            else
                animDirection.y = 1;

            anim.SetBool ("walking", true);
            anim.SetFloat ("SpeedX", movementDirection.x);
            anim.SetFloat ("SpeedY", movementDirection.y);

            Debug.LogFormat ("X: {0}, Y: {1}", movementDirection.x, movementDirection.y);

            if (movementDirection.x > 0)
            {
                anim.SetFloat ("LastMoveX", 1f);
            }
            else if (movementDirection.x < 0)
            {
                anim.SetFloat ("LastMoveX", -1f);
            }
            else
            {
                if (movementDirection.y > 0)
                {
                    anim.SetFloat ("LastMoveY", 1f);
                }
                else if (movementDirection.y < 0)
                {
                    anim.SetFloat ("LastMoveY", -1f);
                }
                else
                {
                    anim.SetFloat ("LastMoveY", 0f);
                }
            }   
        }
        else
        {
                transform.position = Vector3.MoveTowards (transform.position, target, speed * Time.deltaTime);
        }
    }



I have tried adding this into my code (in update) but this happen[https://youtu.be/mJA5E-0iYb4]

    if(transform.position == target)
    {
        anim.SetBool ("walking", false);
    }

Thank you ;D
« Last Edit: July 19, 2016, 10:53:52 AM by XxDivaxX1 »

SPACE GAMER

  • Playmaker Newbie
  • *
  • Posts: 35
    • Portfolio
Re: Unity3D: Returning back to idle state animation Problem
« Reply #1 on: July 19, 2016, 12:00:45 PM »
do u have skype?

XxDivaxX1

  • Playmaker Newbie
  • *
  • Posts: 2
Re: Unity3D: Returning back to idle state animation Problem
« Reply #2 on: July 19, 2016, 03:38:22 PM »
No, why?? ???