Help to find a way to create a condition with IF

Hi. I am using the following free asset: https://assetstore.unity.com/packages/tools/input-management/joystick-pack-107631

And I am trying to trigger some animation based on the distance from the center of the joystick that the user is touching indication the speed. I read the manual and could not find any information that could help me to create a condition. This is what I have now:

using UnityEngine;

public class Player3DExample : MonoBehaviour {

    public float moveSpeed = 8f;
    public Joystick joystick;

    void Update ()
    {
        Vector3 moveVector = (Vector3.right * joystick.Horizontal + Vector3.forward * joystick.Vertical);

        if (moveVector != Vector3.zero)
        {
            transform.rotation = Quaternion.LookRotation(moveVector);
            transform.Translate(moveVector * moveSpeed * Time.deltaTime, Space.World);

            if ( CONDITION HERE )
            {
                FindObjectOfType<AnimationControl>().SetAnimation("isWalking");
            }
            else
            {
                FindObjectOfType<AnimationControl>().SetAnimation("isRunning");
            }
        }  
    }
}

Can someone help me on this? Thanks

Iā€™m assuming looking at your code that this is a top-down game where the character moves in any direction while facing forward all the time such as a time stick shooter.

So what you could do is simply check if either of the sticks has been push past a threshold. You may also want to reset the animation when the sticks are below a dead zone.

    public float moveSpeed = 8f;
    public Joystick joystick;

    private AnimationControl _animControl;

    //How far a joystick should be pushed before considered to be running
    private const float _runThreshold = 0.6f;

    private void Start()
    {
        //Store a reference
        _animControl = FindObjectOfType<AnimationControl>();
    }

    void Update()
    {
        var horizontal = joystick.Horizontal;
        var vertical = joystick.Vertical;

        Vector3 moveVector = (Vector3.right * horizontal + Vector3.forward * vertical);

        if (moveVector != Vector3.zero)
        {
            transform.rotation = Quaternion.LookRotation(moveVector);
            transform.Translate(moveVector * moveSpeed * Time.deltaTime, Space.World);

            //Check if either stick is past the run threshold
            var running = (Mathf.Abs(horizontal) > _runThreshold || Mathf.Abs(vertical) > _runThreshold);

            var animationState = running ? "isRunning" : "isWalking";

            _animControl.SetAnimation(animationState);

        }
    }
1 Like

Thank you. Really appreciate. Have a nice day,