[英]2D basic movement UNITY
目前,我的角色在鍵盤上可以正常工作,但是當我通過3個UI按鈕將您的動作轉換為觸摸時(我也嘗試過UI圖像,但是成功了),我沒有成功
它基本上是向右,向左和跳躍。
請按照以下說明進行操作:
當用戶按下方向鍵時,角色不會停止行走,直到用戶用戶釋放按鈕,並且當您按下跳轉播放器時才跳轉。
這是我用來在鍵盤上移動的腳本!
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 =]
對於“跳轉”動作,您需要一個按鈕。 GameObject-> UI-> Button 。 用您自己的圖像替換該圖像,然后單擊“設置本機大小”。 重新調整按鈕的大小,使其適合您的游戲。
將Button附加到下面的Jump 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
組件,用於他們像跳躍鍵。 您必須實現Virtual JoyStick才能使其看起來逼真。 Unity具有稱為CrossPlatformInputManager的資產,可以做到這一點。 您必須先導入並對其進行一些修改才能使用。 觀看此內容以了解如何導入。
現在,您可以使用CrossPlatformInputManager.GetAxis("Horizontal")
和CrossPlatformInputManager.GetAxisRaw("Horizontal")
替換Input.GetAxis
和Input.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
我認為您應該使用EventTrigger OnPointerDown和OnPointerUp事件:
http://docs.unity3d.com/ScriptReference/EventSystems.EventTrigger.html
希望對您有幫助
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.