0
\$\begingroup\$

i was trying to make a control where the animation plays upon swiping one direction. each time i've make a tap/swipe control, it just keeps popping messages like this. the tap animation works fine, but the animation for swipe controls just doesn't work well, the bool for them has assigned but did not make it 'true' when i swipe a direction, but the bool for the tap works, which is weird.(ignore the miss bool in the animator) picture for animation controller picture of console and it's description about the problem it says that i need to assign the anim variable of the Swipe script in the inspector. but there is nothing left for me to assign, which doesn't make much sense for me. (refer to Region: Animation) picture of inspector

i've tried to make some adjustment with the script, but the results is the same, and it has no problems when build. which is why i need help for the animation stuff.

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

public class Swipe : MonoBehaviour {


public Animator anim;
public bool tap, swipeUp, swipeDown, swipeLeft, swipeRight;
public bool isDragging = false;
public Vector2 startTouch, swipeDelta;



private void Update()
{
    tap = swipeUp = swipeDown = swipeLeft = swipeRight = false;


    #region Standalone Input
    if (Input.GetMouseButtonDown(0))
    {
        tap = true;
        isDragging = true;
        startTouch = Input.mousePosition;
        //Debug.Log("Tapped");
    }
    else if (Input.GetMouseButtonUp(0))
    {
        isDragging = false;
        Reset();
    }

    #endregion

    #region Mobile Inputs
    if (Input.touches.Length >= 1)
    {
        if (Input.touches[0].phase == TouchPhase.Began)
        {
            // tap = true;
            isDragging = true;
            startTouch = Input.touches[0].position;
        }
        else if (Input.touches[0].phase == TouchPhase.Ended || Input.touches[0].phase == TouchPhase.Canceled)
        {
            isDragging = false;
            Reset();
        }
    }


    #endregion



    #region Animation
    if (Tap)
    {
        anim.SetBool("Tap", true);
    }

    if (SwipeDown)
    {
        anim.SetBool("Down", true);
    }

    if (SwipeUp)
    {
        anim.SetBool("Up", true);
    }

    if (SwipeLeft)
    {
        anim.SetBool("Left", true);
    }

    if (SwipeRight)
    {
        anim.SetBool("Right", true);
    }

    #endregion

    //calculating the distance
    swipeDelta = Vector2.zero;
    if(startTouch != Vector2.zero)
    {
        if (isDragging)
        {
            if (Input.touches.Length > 0)
                swipeDelta = Input.touches[0].position - startTouch;
            else if (Input.GetMouseButton(0))
                swipeDelta = (Vector2)Input.mousePosition - startTouch;
        }

    }

    //did we cross the deadzone?
    if (swipeDelta.magnitude > 125)
    {
        //which direction?
        float x = swipeDelta.x;
        float y = swipeDelta.y;
        if (Mathf.Abs(x) > Mathf.Abs(y))
        {
            //left or right
            if (x < 0)
                swipeLeft = true;
            else
                swipeRight = true;
        }
        else
        {
            // Up or down?
            if (y < 0)
                swipeDown = true;
            else
                swipeUp = true;
        }
    }

}


public void Reset()
{
    startTouch = swipeDelta = Vector2.zero;
    isDragging = false;
    anim.SetBool("Tap", false);
    anim.SetBool("Down", false);
    anim.SetBool("Up", false);
    anim.SetBool("Left", false);
    anim.SetBool("Right", false);
}

public Vector2 SwipeDelta { get { return swipeDelta; } }
public bool Tap { get { return tap; } }
public bool SwipeUp { get { return swipeUp; } }
public bool SwipeDown { get { return swipeDown; } }
public bool SwipeLeft { get { return swipeLeft; } }
public bool SwipeRight { get { return swipeRight; } }

}

\$\endgroup\$
  • 1
    \$\begingroup\$ This is usually a sign that you have more than one instance of this script somewhere in your scene. One that's configured correctly: that's the one you're looking at in your screenshot; and also one that's been left with an empty reference: that's the one that's throwing errors. Logging out the name/hierarchy path of the object in Start or in a null check just before using the animator can be a good way to track down this misconfigured duplicate. \$\endgroup\$ – DMGregory Apr 27 at 20:05
  • \$\begingroup\$ Thanks, it was fixed. However, the animation for the swipe still doesn't work.. Seems i'll have to rebuild the script seperately \$\endgroup\$ – Chan Ming Apr 27 at 23:27

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.