目前我的性格完美地在鍵盤上,但是當我轉換你的動作觸碰,通過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這些按鈕=]
爲什麼不使用觸摸輸入?在這裏你可以找到更多的細節:[Input.touches](http://docs.unity3d.com/ScriptReference/Input-touches.html)。 –
從UnityEngine.EventSystems實現IPointerDownHandler和IPointerUpHandler(聽起來比它更糟糕,這裏有很多關於事件系統的教程) – yes
如果在必要時沒有在您的問題中放置代碼,那麼您會被拒絕。很高興你改變了這一點。 – Programmer