Controller Keyboard Mouse TouchPad Unity 3d

Kursus Hari2 Materi1

Controller Keyboard Unity

Keyboard merupakan salah satu controller yang sangat sering digunakan pada game-game pc. Adapun state (keadaan) yang bisa diambil dari keyboard adalah, ketika tombol ditekan, tombol di lepas, dan tombol di tahan.

Umumnya lokasi kode berada di method sbb:
1. Method Update() selalu dipanggil setiap kurang dari 0,1 detik (tiap frame per second). Kondisi ini cocok digunakan untuk mengecek suatu keadaan yang dinginkan setiap waktunya. Seperti keadaan keyboard yang sedang ditekan, ditahan, atau di lepas.

2. Ketika keadaan keyboar ditekan, ditahan atau dilepas, maka percabangan (if else condition) yang ada di dalam method update akan bernilai true dan menjalankan aksi yang ada di dalam percabangan tersebut.

 Contoh:

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour
{
    public void Update()
    {
        if(Input.GetButtonDown("Fire1"))
        {
            Debug.Log(Input.mousePosition);
        }
    }
}
 

using UnityEngine;
using System.Collections;
public class KeyboardInput : MonoBehaviour {
    string myText, myText2;
    public KeyCode myKey;

    void Start () {  }

    void Update () {
        if (Input.GetKeyDown("a")){
            myText = "KeyPress: Tombol A";
        } 
        if (Input.GetKey(KeyCode.B)){
            myText = "KeyDown: Tombol B";
        }
        if (Input.GetKeyUp(myKey)){
            myText = "KeyPress: Tombol pilihan User";
        }
        if (Input.anyKey){
            myText2 = "Ada tombol yang dipencet terus";
        } else {
            myText2 = "";
        }
        if (Input.GetAxis("Fire1")>0){
            myText = "KeyUp: Left Ctrl/Left Mouse";
        }
    }
  
    void OnGUI(){
        GUILayout.BeginArea(new Rect(10,10,200,200));
        GUILayout.Label(myText);
        GUILayout.Label(myText2);
        GUILayout.EndArea();
    }
}  




Controller Mouse Unity

Salah satu Controller yang biasa digunakan untuk game PC adalah mouse. Unity Engine menyediakan beberapa fungsi yang bisa digunakan untuk mendeteksi kondisi dari keadaan mouse. keadaan mouse itu sendiri bisa ane jabarkan, diantaranya, klik kanan, klik kiri, lepas klik kanan, lepas klik kiri dst.

Logika dari Penggunaan Controller Mouse ini adalah sbb:

1. Game Object mengambil posisi mouse. Jika di unity, posisi mouse bisa diambil dalam bentuk World point atau Screen Point. Umumnya, yang dipakai adalah menggunakan world point yang menyatakan posisi mouse yang telah dikonversi ke dalam posisi world dari game.

2. Game Object akan bergerak ke posisi mouse. Pada Unity disediakan fungsi yang bisa digunakan untuk menggerakan Game Object menuju ke target tertentu. Salah satunya menggunakan fungsi MoveTowards(a, b, c). a berisi nilai dari posisi Game Object Sekarang, b berisi target Posisi, c berisi kecepatan dari Game Object untuk menuju Target.


Contoh:

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
    void Update() {
        if (Input.GetMouseButtonDown(0))
            Debug.Log("Pressed left click.");
        
        if (Input.GetMouseButtonDown(1))
            Debug.Log("Pressed right click.");
        
        if (Input.GetMouseButtonDown(2))
            Debug.Log("Pressed middle click.");
        
    }
}
 
atau: 

using UnityEngine;
using System.Collections;
public class MouseInput : MonoBehaviour {
    string myText, myText2, myText3, myText4;
    int number;
    float delta;
    void Start () {
        number = 0;
    }

    void Update () {
        if (Input.GetKey(KeyCode.Mouse0)){
            myText = "Mouse: Tombol Kiri";
        } 
        if (Input.GetMouseButton(1)){
            myText = "Mouse: Tombol Kanan";
        } 
        myText2 = "Position: "+Input.mousePosition.ToString();
        if (Input.mouseScrollDelta.y > 0){
            delta = Input.mouseScrollDelta.y;
            number++;
        } else if (Input.mouseScrollDelta.y < 0) {
            delta = Input.mouseScrollDelta.y;
            number--;
        }
        myText3 = "Scroll: "+delta.ToString();
        myText4 = "Value: "+number.ToString();
    }
    void OnGUI(){
        GUILayout.BeginArea(new Rect(10,10,300,200));
        GUILayout.Label(myText);
        GUILayout.Label(myText2);
        GUILayout.Label(myText3);
        GUILayout.Label(myText4);
        GUILayout.EndArea();
    }
}  



Controller Android Touchpad Unity

using UnityEngine; 
using System.Collections; 
public class AndroidInput : MonoBehaviour { 
    Vector3 tempaccel; 
    Vector2 startpos, currpos; 
    string myText1, myText2, myText3, myText4; 
    int temptouch; 

    void Start () { 
        temptouch = 0;       
    } 

    void Update () { 
        tempaccel = Input.acceleration; 
        if (Input.touchCount > 0){ 
            temptouch++;      
            foreach (Touch t in Input.touches){ 
                switch(t.phase){ 
                case TouchPhase.Began: 
                    startpos = t.position;           
                    break; 
                case TouchPhase.Moved: 
                    currpos = t.position;           
                    break; 
                } 
            } 
        } 
        myText1 = "Total Touch: "+temptouch.ToString(); 
        myText2 = "Acceleration: "+tempaccel.ToString(); 
        myText3 = "StartPos: "+startpos.ToString(); 
        myText4 = "CurrPos: "+currpos.ToString(); 
    } 
    void OnGUI(){ 
        if (GUI.Button(new Rect(Screen.width - 60, 10, 50, 50), "X")){ 
            Application.Quit(); 
        } 
        GUILayout.BeginArea(new Rect(10,10,300,200)); 
        GUILayout.Label(myText1); 
        GUILayout.Label(myText2); 
        GUILayout.Label(myText3); 
        GUILayout.Label(myText4); 
        GUILayout.EndArea(); 
    } 




 Implementasi:

 Tambahkan GamObject 3d-Sphere pada aplikasi dan tambahkan code sbb pada Sphere ybs

using UnityEngine;
using System.Collections;

public class KarakterKontrol : MonoBehaviour {

    // Use this for initialization
    void Start () {
   
    }
   
    // Update is called once per frame
    void Update () {
       
        //menggerakkan objek ke sebelah kanan(Panah arah ke kanan pada keyboard)
        if (Input.GetKey (KeyCode.RightArrow)) {
            transform.Translate(new Vector3(5, 0, 0)*Time.deltaTime);
        }
        //menggerakkan objek ke sebelah kiri(Panah arah ke kiri pada keyboard)
        if (Input.GetKey (KeyCode.LeftArrow)) {
            transform.Translate(new Vector3(-5, 0, 0)*Time.deltaTime);
        }
        //menggerakkan objek ke atas(Panah arah ke atas pada keyboard)
        if (Input.GetKey (KeyCode.UpArrow)) {
            transform.Translate(new Vector3(0, 5, 0)*Time.deltaTime);
        }
        //menggerakkan objek ke bawah(Panah arah ke bawah pada keyboard)
        if (Input.GetKey (KeyCode.DownArrow)) {
            transform.Translate(new Vector3(0, -5, 0)*Time.deltaTime);
        }
    }
}



Hasilnya Objek Sphere 3d akan bergerak gerk sesuai arah panah yang kita tekan.......

Reff:
https://docs.unity3d.com/ScriptReference/Input.html






Controller Keyboard Mouse TouchPad Unity 3d Controller Keyboard Mouse TouchPad Unity 3d Reviewed by ADIARRAY on 20.34 Rating: 5

Tidak ada komentar:

Diberdayakan oleh Blogger.