Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

SOLVED: Dual Joystick Shooter Inputs - How to move with one joystick and shoot with the other

Discussion in 'Editor & General Support' started by jdclark05, Jan 18, 2020.

  1. jdclark05

    jdclark05

    Joined:
    Sep 27, 2019
    Posts:
    1
    Dual Joystick Shooter (one steering, one shooting)


    Using two empty objects a.) "Player" and b.) a child of it named "Shot Spawn," I attached two separate scripts "PlayerController" and "PlayerControllerShot" (because they started colliding when put on the same script *easiest fix*).

    As long as you build your objects (player and shot) to create prefabs and attach your desired scripts like "Destroy/DestroyByContact/Boundary etc etc" they can be plugged in to the public GameObject fields of the following two scripts. These are just for movement and call on nothing other than the occasional rigidbody to rotate or move.

    "PlayerController" Script
    Code (CSharp):
    1. public class PlayerController : MonoBehaviour
    2. {
    3.     public float speed;
    4.     public float tilt;
    5.     public Boundary boundary;
    6.  
    7.     public string horizontalAxis = "Horizontal";
    8.     public string verticalAxis = "Vertical";
    9.    
    10.    
    11.     private Camera mainCamera;
    12.  
    13.  
    14.  
    15.  
    16.  
    17.     void Start ()
    18.     {
    19.         mainCamera = FindObjectOfType<Camera>();
    20.    
    21.    
    22.     }
    23.  
    24.            
    25.  
    26.  
    27.  
    28.     void Update ()
    29.     {
    30.        
    31.         if (GameObject.FindGameObjectWithTag("PlayerModel") == null)
    32.         {
    33.             return;
    34.         }
    35.              
    36.    
    37.     }
    38.  
    39.  
    40.  
    41.  
    42.     void FixedUpdate ()
    43.     {
    44.  
    45.  
    46.         //Calculating speed and movement//
    47.  
    48.         float moveHorizontal = Input.GetAxis ("Horizontal");
    49.         float moveVertical = Input.GetAxis ("Vertical");
    50.  
    51.         Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical).normalized;
    52.  
    53.         GetComponent<Rigidbody>().velocity = movement * speed;
    54.        
    55.         GetComponent<Rigidbody>().position = new Vector3
    56.         (
    57.             //Applying boundaries to player objecct//
    58.  
    59.             Mathf.Clamp (GetComponent<Rigidbody>().position.x, boundary.xMin, boundary.xMax),
    60.             0.0f,
    61.             Mathf.Clamp (GetComponent<Rigidbody>().position.z, boundary.zMin, boundary.zMax)
    62.         );
    63.        
    64.         GetComponent<Rigidbody>().rotation = Quaternion.Euler (0.0f, 0.0f, GetComponent<Rigidbody>().velocity.x * -tilt);
    65.     }
    66.  
    67.  
    68. }
    69.  




    and the "PlayerControllerShot" Script
    Code (CSharp):
    1. public class PlayerControllerShot : MonoBehaviour
    2. {
    3.  
    4.  
    5.     public GameObject shot;
    6.  
    7.  
    8.     public string horizontalAxis2 = "Horizontal2";
    9.     public string verticalAxis2 = "Vertical2";
    10.     private bool canShoot = true;
    11.  
    12.  
    13.  
    14.     void Update ()
    15.     {
    16.  
    17.         if (GameObject.FindGameObjectWithTag("PlayerModel") == null)
    18.         {
    19.             return;
    20.         }
    21.  
    22.  
    23.  
    24.         Vector3 shootDirection = Vector3.right*Input.GetAxis(horizontalAxis2) + Vector3.forward*Input.GetAxis(verticalAxis2);
    25.         if(canShoot && shootDirection.sqrMagnitude > 0.0f)
    26.         {
    27.             transform.rotation = Quaternion.LookRotation(shootDirection,Vector3.up);
    28.             Instantiate(shot,transform.position,transform.rotation);
    29.  
    30.      
    31.         }
    32.        
    33.  
    34.     }
    This, along with setting up the two separate Joystick Inputs let me use one joystick to move and the other to shoot.
     

    Attached Files:

    • 1.jpg
      1.jpg
      File size:
      182 KB
      Views:
      469
    • 2.jpg
      2.jpg
      File size:
      174.9 KB
      Views:
      465
    Last edited: Jan 18, 2020