Search Unity

How can I make joystick for my game?

Discussion in 'General Discussion' started by superpou1filip, May 11, 2020.

  1. superpou1filip

    superpou1filip

    Joined:
    May 11, 2020
    Posts:
    1
    I am making a 3D game. I want also to port it to Android and IOS. So for smartphones I will need on-screen controller. My character actually can only walk and jump, so i need joystick and button. Here's my script that works with keyboard :



    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerController : MonoBehaviour
    6. {
    7.     public float moveSpeed;
    8.     public float jumpForce;
    9.     public CharacterController controller;
    10.     private Vector3 moveDirection;
    11.     public float gravityScale;
    12.     // Start is called before the first frame update
    13.     void Start()
    14.     {
    15.        
    16.         controller = GetComponent<CharacterController>();
    17.     }
    18.  
    19.     // Update is called once per frame
    20.     void Update()
    21.     {
    22.        
    23.         float yStore = moveDirection.y;
    24.        
    25.         moveDirection = (transform.forward * Input.GetAxisRaw("Vertical") ) + (transform.right * Input.GetAxisRaw("Horizontal") );
    26.         moveDirection = moveDirection.normalized * moveSpeed;
    27.         moveDirection.y = yStore;
    28.         if (controller.isGrounded)
    29.         {
    30.  
    31.  
    32.             moveDirection.y = 0f;
    33.             if (Input.GetButtonDown("Jump"))
    34.             {
    35.                 moveDirection.y = jumpForce;
    36.             }
    37.  
    38.         }
    39.             moveDirection.y = moveDirection.y + (Physics.gravity.y * gravityScale * Time.deltaTime);
    40.             controller.Move(moveDirection * Time.deltaTime);
    41.  
    42.        
    43.     }
    44. }
    45.  
     
  2. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,573
    Googling for "Unity 3d mobile input joystick" results in more than one tutorial.