2014-07-06 28 views
0

得到這個簡單的字符控制器,使我的平臺遊戲角色左右移動。向角色控制器添加觸摸控件? Unity2d

希望有人可以整合或告訴我如何爲android/ios添加觸摸控件。

只需簡單觸摸左側屏幕左右兩側即可右轉。

感謝

using UnityEngine; 
using System.Collections; 

public class RobotController : MonoBehaviour { 
//This will be our maximum speed as we will always be multiplying by 1 
public float maxSpeed = 2f; 
//a boolean value to represent whether we are facing left or not 
bool facingRight = false; 
//a value to represent our Animator 
Animator anim; 
// Use this for initialization 
void Start() { 
    //set anim to our animator 
    anim = GetComponent<Animator>(); 

} 

// Update is called once per frame 
void FixedUpdate() { 

    float move = Input.GetAxis ("Horizontal");//Gives us of one if we are moving via the arrow keys 
    //move our Players rigidbody 
    rigidbody2D.velocity = new Vector3 (move * maxSpeed, rigidbody2D.velocity.y); 
    //set our speed 
    anim.SetFloat ("Speed",Mathf.Abs (move)); 
    //if we are moving left but not facing left flip, and vice versa 
    if (move < 0 && !facingRight) { 

     Flip(); 
    } else if (move > 0 && facingRight) { 
     Flip(); 
    } 
} 

//flip if needed 
void Flip(){ 
    facingRight = !facingRight; 
    Vector3 theScale = transform.localScale; 
    theScale.x *= -1; 
    transform.localScale = theScale; 
} 
} 

回答

0

在Update()方法,你要使用foreach循環,其通過在特定的幀更新所有觸摸事件循環。

foreach(Touch touch in Input.touches) 
{ 
    if(leftButton.HitTest(touch.position)) 
    { 
     //move character left 
    } 

    if(rightButton.HitTest(touch.position)) 
    { 
     //move character right 
    } 
} 

這是假設你在start()方法設置你的左,右控件中兩個GUITexture按鈕。

+0

Rahin,謝謝你的迴應。所以我可以在屏幕的每一側製作兩個巨大的透明按鈕,我如何將您的代碼鏈接到這些對象? – Exilekiller