Search Unity

Resolved Help to find a way to create a condition with IF

Discussion in 'Scripting' started by khrysller, May 10, 2020.

  1. khrysller

    khrysller

    Joined:
    Mar 14, 2019
    Posts:
    125
    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:

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class Player3DExample : MonoBehaviour {
    4.  
    5.     public float moveSpeed = 8f;
    6.     public Joystick joystick;
    7.  
    8.     void Update ()
    9.     {
    10.         Vector3 moveVector = (Vector3.right * joystick.Horizontal + Vector3.forward * joystick.Vertical);
    11.  
    12.         if (moveVector != Vector3.zero)
    13.         {
    14.             transform.rotation = Quaternion.LookRotation(moveVector);
    15.             transform.Translate(moveVector * moveSpeed * Time.deltaTime, Space.World);
    16.  
    17.             if ( CONDITION HERE )
    18.             {
    19.                 FindObjectOfType<AnimationControl>().SetAnimation("isWalking");
    20.             }
    21.             else
    22.             {
    23.                 FindObjectOfType<AnimationControl>().SetAnimation("isRunning");
    24.             }
    25.         }  
    26.     }
    27. }
    Can someone help me on this? Thanks
     
  2. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    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.

    Code (CSharp):
    1.     public float moveSpeed = 8f;
    2.     public Joystick joystick;
    3.  
    4.     private AnimationControl _animControl;
    5.  
    6.     //How far a joystick should be pushed before considered to be running
    7.     private const float _runThreshold = 0.6f;
    8.  
    9.     private void Start()
    10.     {
    11.         //Store a reference
    12.         _animControl = FindObjectOfType<AnimationControl>();
    13.     }
    14.  
    15.     void Update()
    16.     {
    17.         var horizontal = joystick.Horizontal;
    18.         var vertical = joystick.Vertical;
    19.  
    20.         Vector3 moveVector = (Vector3.right * horizontal + Vector3.forward * vertical);
    21.  
    22.         if (moveVector != Vector3.zero)
    23.         {
    24.             transform.rotation = Quaternion.LookRotation(moveVector);
    25.             transform.Translate(moveVector * moveSpeed * Time.deltaTime, Space.World);
    26.  
    27.             //Check if either stick is past the run threshold
    28.             var running = (Mathf.Abs(horizontal) > _runThreshold || Mathf.Abs(vertical) > _runThreshold);
    29.  
    30.             var animationState = running ? "isRunning" : "isWalking";
    31.  
    32.             _animControl.SetAnimation(animationState);
    33.  
    34.         }
    35.     }
     
    khrysller likes this.
  3. khrysller

    khrysller

    Joined:
    Mar 14, 2019
    Posts:
    125
    Thank you. Really appreciate. Have a nice day,