Search Unity

how to get current angel of a analog stick

Discussion in 'Scripting' started by CRISTALSONIC, Jul 1, 2022.

  1. CRISTALSONIC

    CRISTALSONIC

    Joined:
    Sep 22, 2016
    Posts:
    136
    as the title suggest im looking to get the current direction of analog input.

    long and short im patching someone else code and in my changes I am adding controller input to a game that only has mouse input. I notice the rotation is handled with.
    Code (CSharp):
    1.  Vector3 positionOnScreen = Camera.main.WorldToScreenPoint(transform.position);
    2. angle = AngleBetweenTwoPoints(positionOnScreen, Input.mousePosition);
    3. Quaternion newRotation = Quaternion.Euler(angle, 90, 90);
    4. Vector3 newPosition = transform.position + (ShipDirection * Time.fixedDeltaTime);
    5. newPosition.z = 0;
    6. transform.rotation = newRotation;
    7.  transform.position = newPosition;
    as the name suggest AngleBetweenTwoPoints will calculate a float in degrees for angle to be inserted into newRotation = Quaternion.Euler(angle, 90, 90); ( comparing mouse position to player position )
    when testing with a mouse the angles are calculated as
    • far right angle = 180 ,
    • far left angle = 360,
    • top of screen angle = 270 ,
    • bottom of screen angle= 450
    so for constancy I can say
    • if x >0 angle= 180 ,
    • if x <0 angle= 360 ,
    • if Y >0 angle= 270 ,
    • if Y <0 angle= 450 ,
    the part im stuck un is how would i get the in-between angles ?

    so for example if the stick is pressed in the top right it should be an angle between 180 and 270 but how would I get the precise value from the current xy access .

    any feed back would be appreciated
     
  2. FlashMuller

    FlashMuller

    Joined:
    Sep 25, 2013
    Posts:
    451
    Maybe I'm naive, but can't you just keep using the functions you have? Like: Take the individual Axis-Inputs of your controller, make them a Vector2 and then use the already existing Functions.
    Code (CSharp):
    1.  
    2. angle = AngleBetweenTwoPoints(Vector2.zero, myControllerVector)
    3.  
    Getting the Input depends on the system you are using. Legacy or InputManager.
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    Mathf.Atan2()
    and
    Mathf.Rad2Deg
    can give you every angle your heart desires.
     
  4. CRISTALSONIC

    CRISTALSONIC

    Joined:
    Sep 22, 2016
    Posts:
    136
    using Legacy input but that part is not an issue , "Take the individual Axis-Inputs of your controller, make them a Vector2" I will have to double check the parameter of that function when I get a chance , I could be over thinking it , thanks


    Mathf.Atan2() and Mathf.Rad2Deg , gassing Atan is the tangent from geometry ( never got a good grasp on that class) Rad2Deg speaks for its self good to know that is built in

    thanks guys
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    It is an octant-corrected wrapper around
    Mathf.Atan()
    (inverse of tangent, or arc tangent)