Trakteer

Player Bergerak Tidak Lebih Dari Batas Layar Android Unity

  transform.position = new Vector3(120f, 50f, 45f);


========================================

public float PlayerSpeed;

public float maxPos = 2.4f; // batas jalan player

Vector3 position; //posisi pada jalan

public uiManager ui ;//memanggil fungsi uimanager

bool currntPlatformAndroid = false;

Rigidbody2D rb;


void Awake (){

rb = GetComponent<Rigidbody2D> ();//mengakses rigibody player

//control menggunakan android

#if UNITY_ANDROID

currntPlatformAndroid = true;

#else

currntPlatformAndroid = false

#endif


}


// Use this for initialization

void Start () {

//ui = GetComponent<uiManager> ();//memanggil fungsi uimanager

position = transform.position; // posisi player pada jalan

if (currntPlatformAndroid == true) {

Debug.Log ("Android");

} else {

Debug.Log("Windows");

}

}

// Update is called once per frame

void Update () {

if (currntPlatformAndroid == true) {

//kosongi jika menggunakan tombol atau keyboard

//TouchMove();//menggrakkan player di Android

AcclerometerMove();//menggerakan dengan sensor android


} else {

//menggrakkan player di windows

position.x += Input.GetAxis ("Horizontal") * PlayerSpeed * Time.deltaTime;


position.x = Mathf.Clamp (position.x, -2.4f, 2.4f);


transform.position = position;

}

position = transform.position;

position.x = Mathf.Clamp (position.x, -2.4f, 2.4f);

transform.position = position;

}


void OnCollisionEnter2D(Collision2D col){


if (col.gameObject.tag == "EnemyPlayer") { //jika player menabrak objek dengan tag enemyplayer

//Destroy (gameObject);//player hilang

gameObject.SetActive(false);

ui.gameOverActivated();//mengaktifkan gameover jadi true

}

}


//mengontrol mengunakan sensor accel android

void AcclerometerMove(){

float x = Input.acceleration.x;

//Debug.Log ("x = ")+x;//check sensor bekerja


if (x < -0.1f) {

MoveLeft ();

} else if (x > 0.1f) {

MoveRight ();

} else {

SetVelocityZero();

}

}


//control mengunakan layar touch screen

void TouchMove(){

if (Input.touchCount > 0) {

Touch touch = Input.GetTouch (0);

float middle = Screen.width / 2;

if (touch.position.x < middle && touch.phase == TouchPhase.Began) {

MoveLeft ();

} else if (touch.position.x > middle && touch.phase == TouchPhase.Began) {

MoveRight ();

}

} else {

SetVelocityZero();

}

}


//control menggunakan tombol

public void MoveLeft(){

rb.velocity = new Vector2 (-PlayerSpeed, 0);

}

public void MoveRight(){

rb.velocity = new Vector2 (PlayerSpeed, 0);

}

public void SetVelocityZero(){

rb.velocity = Vector2.zero;

}


Mohon Aktifkan Javascript!Enable JavaScript