Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Help with which way the joystick is facing

Discussion in 'Scripting' started by AxeyPm, Dec 22, 2021.

  1. AxeyPm

    AxeyPm

    Joined:
    May 15, 2021
    Posts:
    4
    hello , im looking for help with which way the joystick is going for example , top , top right , right , bottom right , ect... , it is for my animations as each one is linked to it , i know it requires checking the horizontal + verticle input , but not sure how i would check for example top would be -0.35 < joystick.Horizontal && 0.35 > joystick.Horizontal && joystick.Vertical > 0.93 && joystick.Vertical > 0.93 , but this is when joystick is fully up , but if pushed up a tiny bit it wouldnt register as top
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,962
    Two problems:

    Don't consider any input until the total magnitude of the X/Y vector is greater than a certain amount

    Use Mathf.Abs() to decide if X or Y is the bigger deflection axis.

    Combining it:

    Code (csharp):
    1. // THIS IS NOT CODE, it is python-ish pseudocode:
    2. if XY.magnitude > threshhold:
    3.   if X > Y:
    4.     if X < 0: left
    5.     if X > 0: right
    6.   if Y > X:
    7.     if Y < 0: down
    8.     if Y > 0: up
    9.  
     
    AxeyPm likes this.
  3. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Convert your direction vector to a 0-359 degree angle, then divide that by 45 and floor it to get a value from 0-7 mapping to your cardinal/ordinal directions.
     
    AxeyPm likes this.
  4. AxeyPm

    AxeyPm

    Joined:
    May 15, 2021
    Posts:
    4
    could you check the object to see what angle he is moving at? if so how would that be done?

    edit : nevermind got it
    float angle = Mathf.Atan2(joystick.Vertical, joystick.Horizontal) * Mathf.Rad2Deg;
     
    Last edited: Dec 22, 2021