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

Resolved Hi, I need help implementing a joystick, ty!

Discussion in 'Scripting' started by Silvaantonio, Apr 29, 2021.

  1. Silvaantonio

    Silvaantonio

    Joined:
    Apr 29, 2021
    Posts:
    9
    Hi, i currently have this as movement controls:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerController : MonoBehaviour
    6. {
    7.     public PlayerState curState;            // current player state
    8.  
    9.     // values
    10.     public float moveSpeed;                 // force applied horizontally when moving
    11.     public float flyingSpeed;               // force applied upwards when flying
    12.     public bool grounded;                   // is the player currently standing on the ground?
    13.     public float stunDuration;              // duration of a stun
    14.     private float stunStartTime;            // time that the player was stunned
    15.  
    16.     // components
    17.     public Rigidbody2D rig;                 // Rigidbody2D component
    18.     public Animator anim;                   // Animator component
    19.     public ParticleSystem jetpackParticle;  // ParticleSystem of jetpack
    20.  
    21.     void FixedUpdate ()
    22.     {
    23.         grounded = IsGrounded();
    24.         CheckInputs();
    25.  
    26.         // is the player stunned?
    27.         if(curState == PlayerState.Stunned)
    28.         {
    29.             // has the player been stunned for the duration?
    30.             if(Time.time - stunStartTime >= stunDuration)
    31.             {
    32.                 curState = PlayerState.Idle;
    33.             }
    34.         }
    35.     }
    36.  
    37.     // checks for user input to control player
    38.     void CheckInputs ()
    39.     {
    40.         if (curState != PlayerState.Stunned)
    41.         {
    42.             // movement
    43.             Move();
    44.  
    45.             // flying
    46.             if (Input.GetKey(KeyCode.UpArrow))
    47.                 Fly();
    48.             else
    49.                 jetpackParticle.Stop();
    50.         }
    51.  
    52.         // update our current state
    53.         SetState();
    54.     }
    55.  
    56.     // sets the player's state
    57.     void SetState ()
    58.     {
    59.         // don't worry about changing states if the player's stunned
    60.         if (curState != PlayerState.Stunned)
    61.         {
    62.             // idle
    63.             if (rig.velocity.magnitude == 0 && grounded)
    64.                 curState = PlayerState.Idle;
    65.             // walking
    66.             if (rig.velocity.x != 0 && grounded)
    67.                 curState = PlayerState.Walking;
    68.             // flying
    69.             if (rig.velocity.magnitude != 0 && !grounded)
    70.                 curState = PlayerState.Flying;
    71.         }
    72.  
    73.         // tell the animator we've changed states
    74.         anim.SetInteger("State", (int)curState);
    75.     }
    76.  
    77.     // moves the player horizontally
    78.     void Move ()
    79.     {
    80.         // get horizontal axis (A & D, Left Arrow & Right Arrow)
    81.         float dir = Input.GetAxis("Horizontal");
    82.  
    83.         // flip player to face the direction they're moving
    84.         if (dir > 0)
    85.             transform.localScale = new Vector3(1, 1, 1);
    86.         else if (dir < 0)
    87.             transform.localScale = new Vector3(-1, 1, 1);
    88.  
    89.         // set rigidbody horizontal velocity
    90.         rig.velocity = new Vector2(dir * moveSpeed, rig.velocity.y);
    91.     }
    92.  
    93.     // adds force upwards to player
    94.     void Fly ()
    95.     {
    96.         // add force upwards
    97.         rig.AddForce(Vector2.up * flyingSpeed, ForceMode2D.Impulse);
    98.  
    99.         // play jetpack particle effect
    100.         if (!jetpackParticle.isPlaying)
    101.             jetpackParticle.Play();
    102.     }
    103.  
    104.     // called when the player gets stunned
    105.     public void Stun ()
    106.     {
    107.         curState = PlayerState.Stunned;
    108.         rig.velocity = Vector2.down * 3;
    109.         stunStartTime = Time.time;
    110.         jetpackParticle.Stop();
    111.     }
    112.  
    113.     // returns true if player is on ground, false otherwise
    114.     bool IsGrounded ()
    115.     {
    116.         // shoot a raycast down underneath the player
    117.         RaycastHit2D hit = Physics2D.Raycast(new Vector2(transform.position.x, transform.position.y - 0.85f), Vector2.down, 0.3f);
    118.  
    119.         // did we hit anything?
    120.         if(hit.collider != null)
    121.         {
    122.             // was it the floor?
    123.             if(hit.collider.CompareTag("Floor"))
    124.             {
    125.                 return true;
    126.             }
    127.         }
    128.  
    129.         return false;
    130.     }
    131.  
    132.     // called when the player enters another object's collider
    133.     void OnTriggerEnter2D (Collider2D col)
    134.     {
    135.         // if the player isn't already stunned, stun them if the object was an obstacle
    136.         if(curState != PlayerState.Stunned)
    137.         {
    138.             if(col.CompareTag("Obstacle"))
    139.             {
    140.                 Stun();
    141.             }
    142.         }
    143.     }
    144. }
    145.  
    146. public enum PlayerState
    147. {
    148.     Idle,       // 0
    149.     Walking,    // 1
    150.     Flying,     // 2
    151.     Stunned     // 3
    152. }
    I want to move it with a joystick, i have created a canvas, and chossen a joystick i just dont know how tom make my player move with it. Any help ty :)
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
  3. Silvaantonio

    Silvaantonio

    Joined:
    Apr 29, 2021
    Posts:
    9
    I tried following that video, but i wasn't able to move my player given that he doesn't jump or move normally he flyes
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    Somewhere you gather user intent from keys.

    You want to gather the user intent from joystick.

    In either case, you gather the intent however is appropriate.

    By "gather" I mean generally obtain it into local variables.

    Now when the gathering step is done, move onto processing it, which could mean "make him fly, make him walk, make him jump," whatever is appropriate.

    Looking at your code above you have intertwined the gathering of intention with the processing of intention. I suggest you disentangle that so that ALL the user intent is gathered in one step, then processed in a second step.

    This would let you easily add in joystick input between those two steps.
     
    Joe-Censored likes this.
  5. Silvaantonio

    Silvaantonio

    Joined:
    Apr 29, 2021
    Posts:
    9
    Ok ty for the help ! Ill try :)