【发布时间】:2016-04-25 07:42:10
【问题描述】:
目前我的角色在键盘上完美运行,但是当我通过 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 =]
【问题讨论】:
-
为什么不使用触摸输入?在这里您可以找到更多详细信息:Input.touches.
-
从 UnityEngine.EventSystems 实现 IPointerDownHandler 和 IPointerUpHandler (听起来比它还糟糕,事件系统的教程很多)
-
如果您在必要时没有在问题中添加代码,您将被否决。很高兴你改变了这一点。
标签: c# android unity3d unityscript