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. Dismiss Notice

Rotate a "FirePoint" according to inputs!

Discussion in 'Scripting' started by Ciava, Sep 14, 2020.

  1. Ciava

    Ciava

    Joined:
    Jun 1, 2018
    Posts:
    50
    Hello ... when I thought It was all Ok ... I imported a free touch control to use my "game" on a phone.

    I don't know how to solve this problem: (It's a "Top-Down" scenario)

    How can I rotate my "Firepoint" according to player direction?

    When using keyboard I did a simple reading of X and Y axis that could be just " -1, 0, 1". Now with Joystick I am having a problem!
    When moving with fingers, of course I can't have a "perfect zero" on one of the axis and so all my "if statements" are gone wrong!

    Code (CSharp):
    1.  if ((movement.x > 0.01) && (movement.y == 0))
    2.         {
    3.             Vector3 eu = new Vector3(0, 0, 0);
    4.             firePoint.transform.rotation = Quaternion.Euler(eu);
    5.         }
    6.  
    7.         if ((movement.x < -0.01) && (movement.y == 0))
    8.         {
    9.             Vector3 eu = new Vector3(0, 0, 180);
    10.             firePoint.transform.rotation = Quaternion.Euler(eu);
    11.         }
    ... and so on ....

    I am not only a noob ... I am also quite stupid! :-D I can't find a solution to rotate that fire-point while I, ... Eg. describe circles with my fingers and player do circles too. I will post a simple PIC in which you can see both Joystick and Player. In this case, as "the noose" of the player is facing left, also FirePoint rotating (Red Arrow) is on the left. (Don't mind pink enemy strange shape...).

    To move player I used this:

    Code (CSharp):
    1. movement.x = TCKInput.GetAxis("Joystick0", EAxisType.Horizontal);
    2. movement.y = TCKInput.GetAxis("Joystick0", EAxisType.Vertical);
    and then:

    Code (CSharp):
    1.         rb.MovePosition(rb.position + movement * speedMove * Time.fixedDeltaTime);
    2.  

    How can that FirePoint's "red arrow" always point ahead, "out of the noose" of my player?

    As usual ... thank you very much for your help!

    G.
     

    Attached Files:

  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,336
    I approach this by making a dead zone, or "zero zone."

    I gather x and y inputs (from wherever), and if the magnitude of that vector is less than a small amount (say 0.05f or 0.1f), then set the collected inputs to zero.

    This has the side benefit of guarding against creepy joysticks that don't quite zero out.

    And most obviously, if the input is zero for any reason, you simply do not set direction, leaving it where it already was.
     
  3. Ciava

    Ciava

    Joined:
    Jun 1, 2018
    Posts:
    50
    Hi Kurt and thanks for replying!

    Ehm, I understood what you mean ... but I don't know how to read an axis magnitude (to then set it to 0 if less than that small amount ... :oops:
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,336
    Something like:

    Code (csharp):
    1. movement ..... however you already gather it...
    2.  
    3. if (movement.magnitude < 0.1f) movement = Vector3.zero;
     
  5. Ciava

    Ciava

    Joined:
    Jun 1, 2018
    Posts:
    50
    No .. I can't do It. :-( May be I did not understand or I did not explained myself right.

    If I push "Right Key" I'll have y = 1 and x = 0 and that's fine ... the problem is with touch controls

    if I drag Joystick right I will have y = 1 and x = 0.0something and this will "crash" my if statements to rotate FirePoint.

    Doesn't magnitude refers to the "speed" the player is moving? If I go y = 1 and x = 0.0something ... my RigidBody2D will have a near 1 magnitude due to the left (y=1) full movement!

    If I push up my touch joystick I'd need the "red arrow" rotation to be upward. If I slightly drag my finger clockwise, rotation should follow and rotate clockwise (while player change direction) ... unfortunately I don't know how to achieve this!