2016-04-25 109 views
-1

目前我的性格完美地在鍵盤上,但是當我轉換你的動作觸碰,通過3個UI按鈕(我試過UI圖像過,但成功)我是沒有成功2D基本動作UNITY

它基本上向右,向左,跳躍。

應該怎麼做,使之按照下列指示:

當用戶按下方向,字符不會停止行走,直到用戶用戶釋放按鈕,當你按下的跳高運動員,跳躍。

這是我用來移動鍵盤的腳本!

using UnityEngine; 
using System.Collections; 

public class Player : MonoBehaviour 
{ 

    public float velocity; 

    public Transform player; 
    private Animator animator; 

    public bool isGrounded; 
    public float force; 

    public float jumpTime = 0.4f; 
    public float jumpDelay = 0.4f; 
    public bool jumped = false; 
    public Transform ground; 

    private Gerenciador gerenciador; 

    // Use this for initialization 
    void Start() 
    { 
     gerenciador = FindObjectOfType (typeof(Gerenciador)) as Gerenciador; 
     animator = player.GetComponent<Animator>(); 
     gerenciador.StartGame(); 
    } 

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

     Move(); 

    } 

    void Move() 
    { 

     isGrounded = Physics2D.Linecast (this.transform.position, ground.position, 1 << LayerMask.NameToLayer ("Floor")); 
     animator.SetFloat ("run", Mathf.Abs (Input.GetAxis ("Horizontal"))); 
     if (Input.GetAxisRaw ("Horizontal") > 0) { 

      transform.Translate (Vector2.right * velocity * Time.deltaTime); 
      transform.eulerAngles = new Vector2 (0, 0); 
     } 
     if (Input.GetAxisRaw ("Horizontal") < 0) { 

      transform.Translate (Vector2.right * velocity * Time.deltaTime); 
      transform.eulerAngles = new Vector2 (0, 180); 
     } 

     if (Input.GetButtonDown ("Vertical") && isGrounded && !jumped) { 

      // rigidbody2D.AddForce (transform.up * force); 
      GetComponent<Rigidbody2D>().AddForce (transform.up * force); 
      jumpTime = jumpDelay; 
      animator.SetTrigger ("jump"); 
      jumped = true; 
     } 

     jumpTime -= Time.deltaTime; 

     if (jumpTime <= 0 && isGrounded && jumped) { 

      animator.SetTrigger ("ground"); 
      jumped = false; 

     } 
    } 
} 

C#如果可能的話,記得我是使用Canvas UI這些按鈕=]

+0

爲什麼不使用觸摸輸入?在這裏你可以找到更多的細節:[Input.touches](http://docs.unity3d.com/ScriptReference/Input-touches.html)。 –

+0

從UnityEngine.EventSystems實現IPointerDownHandler和IPointerUpHandler(聽起來比它更糟糕,這裏有很多關於事件系統的教程) – yes

+0

如果在必要時沒有在您的問題中放置代碼,那麼您會被拒絕。很高興你改變了這一點。 – Programmer

回答

2

對於跳轉動作,你需要的是一個按鈕。 GameObject-> UI-> Button。用您自己的圖像替換圖像,然後單擊「設置原始大小」。將按鈕重新調整爲適合您遊戲的尺寸。

將按鈕附加到跳轉按鈕下面的插槽腳本。

public Button jumpButton; 

void jumpButtonCallBack() 
{ 
    // rigidbody2D.AddForce (transform.up * force); 
    GetComponent<Rigidbody2D>().AddForce(transform.up * force); 
    jumpTime = jumpDelay; 
    animator.SetTrigger("jump"); 
    jumped = true; 
} 


void OnEnable(){ 
    //Un-Register Button 
    jumpButton.onClick.AddListener(() => jumpButtonCallBack()); 
} 

void OnDisable(){ 
    //Un-register Button 
    jumpButton.onClick.RemoveAllListeners(); 
} 

對於移動按鈕,你應該使用Button組件爲他們喜歡跳躍鍵。您必須實施虛擬JoyStick才能看起來切合實際。 Unity有資產稱爲CrossPlatformInputManager,可以做到這一點。你必須導入它並修改它以便使用它。觀看this瞭解如何導入它。

現在,您可以用CrossPlatformInputManager.GetAxis("Horizontal")CrossPlatformInputManager.GetAxisRaw("Horizontal")替換Input.GetAxisInput.GetAxisRaw函數。

如果你得到它的工作,然後可以使用下面的代碼,使您的代碼兼容移動和桌面。

#if UNITY_EDITOR || UNITY_STANDALONE || UNITY_WEBGL 
//put your Input.GetAxis` and `Input.GetAxisRaw` code here 
#elif UNITY_ANDROID || UNITY_IOS 
//Put your `CrossPlatformInputManager.GetAxis("Horizontal")` and `CrossPlatformInputManager.GetAxisRaw("Horizontal")`. here 
#endif 
+1

另一次你給我最好的遠方! Ty再次=] –