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

How to connect joystick?

Discussion in 'Scripting' started by gamestudiovoron, Mar 25, 2019.

  1. gamestudiovoron

    gamestudiovoron

    Joined:
    Jul 20, 2018
    Posts:
    9
    Hey, guys.Help please.I can not connect the script to work with a joystick.The character turns and directs the weapon towards the mouse.How to connect to the joystick?


    Code (CSharp):
    1. public void Shoot()
    2.     {
    3.  
    4.  
    5.      
    6.         GameObject projectile = (GameObject)Instantiate(Bullet, Shooter.transform.position, Quaternion.identity);    
    7.         Vector3 mousePos = Input.mousePosition;
    8.         mousePos.z = 0;    
    9.         Vector3 objectPos = Camera.main.WorldToScreenPoint (transform.position);
    10.         mousePos.x = mousePos.x - objectPos.x;
    11.         mousePos.y = mousePos.y - objectPos.y;
    12.         mousePos.Normalize();
    13.         projectile.GetComponent<Rigidbody2D>().velocity = new Vector2 (shootVelocity * mousePos.x, shootVelocity * mousePos.y + Random.Range (-10.5f, 10.5f));
    14.         canShoot = false;
    15.    
    16.     }
    17.  
    18.     void Update ()
    19.     {
    20.      
    21.    
    22.         Vector3 mousePos = Input.mousePosition;
    23.         mousePos.z = 0;
    24.      
    25.         Vector3 objectPos = Camera.main.WorldToScreenPoint (transform.position);
    26.         mousePos.x = mousePos.x - objectPos.x;
    27.         mousePos.y = mousePos.y - objectPos.y;
    28.         mousePos.Normalize();
    29.  
    30.         float angle = Mathf.Atan2 (mousePos.y, mousePos.x) * Mathf.Rad2Deg;
    31.  
    32.         shootVelocity = 90;
    33.         ShootCDTime = 0.4f;
    34.  
    35.         if(angle <= 90f & angle >= -90f )
    36.         {
    37.             player.gameObject.transform.localScale = new Vector3(1f, 1f, 1f);
    38.             this.gameObject.transform.localScale = new Vector3(1f, 1f, 1f);
    39.             transform.rotation = Quaternion.Euler (new Vector3 (0, 0, angle));
    40.         }
    41.         else
    42.         {
    43.             player.gameObject.transform.localScale = new Vector3(-1f, 1f, 1f);
    44.             this.gameObject.transform.localScale = new Vector3(-1f, -1f, 1f);
    45.             transform.rotation = Quaternion.Euler (new Vector3 (0, 0, angle));
    46. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,749
    Use Input.GetAxis("Horizontal") and/or "Vertical" to read joystick input. NOTE: the joystick has to already be successfully connected and recognized by whatever OS you are running.
     
  3. gamestudiovoron

    gamestudiovoron

    Joined:
    Jul 20, 2018
    Posts:
    9
    I know that to connect GetAxis ("Horizontal") and/or " Vertical,but I do not understand how to register it in the script.Sorry for my English.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,749
    Lots of tutorials out there. This is one:



    You get a floating point value back, you decide how to scale and act on it and move / control your character.

    The primary hurdle you face is that the mouse you are using is positional (immediate here) whereas the joystick is relative motion (go left, go right), but the tutorial shows how to use the relative motion a joystick gives you.
     
  5. gamestudiovoron

    gamestudiovoron

    Joined:
    Jul 20, 2018
    Posts:
    9
    Thanks a lot.:)
     
    Kurt-Dekker likes this.