C# 二维基本运动统一
目前,我的角色在键盘上工作得很好,但当我通过3个UI按钮将你的动作转换为触摸时(我也尝试了UI图像,但成功了),我没有成功 它基本上是向右,向左,然后跳跃 如何使其遵循以下说明: 当用户按下方向键时,角色不会停止行走,直到用户释放按钮,并且当您按下跳跃播放器时,角色才会停止行走 这是我用来在键盘上移动的脚本C# 二维基本运动统一,c#,android,unity3d,unityscript,C#,Android,Unity3d,Unityscript,目前,我的角色在键盘上工作得很好,但当我通过3个UI按钮将你的动作转换为触摸时(我也尝试了UI图像,但成功了),我没有成功 它基本上是向右,向左,然后跳跃 如何使其遵循以下说明: 当用户按下方向键时,角色不会停止行走,直到用户释放按钮,并且当您按下跳跃播放器时,角色才会停止行走 这是我用来在键盘上移动的脚本 using UnityEngine; using System.Collections; public class Player : MonoBehaviour { public
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;
}
}
}
使用UnityEngine;
使用系统集合;
公共类玩家:单一行为
{
公众漂浮速度;
公共转换播放器;
私人动画师;
公共学校停课;
公众浮力;
公共浮点数跳时=0.4f;
公共浮动跳线延迟=0.4f;
公共bool=false;
公共转换场地;
私人基尔尼亚多基尔尼亚多;
//用于初始化
无效开始()
{
gerenciador=作为gerenciador的FindObjectOfType(typeof(gerenciador))类型;
animator=player.GetComponent();
gerenciador.StartGame();
}
//每帧调用一次更新
无效更新()
{
移动();
}
无效移动()
{
isground=Physics2D.Linecast(this.transform.position,ground.position,1 0){
transform.Translate(Vector2.right*速度*时间.deltaTime);
transform.eulerAngles=新向量2(0,0);
}
if(Input.GetAxisRaw(“水平”)<0){
transform.Translate(Vector2.right*速度*时间.deltaTime);
transform.eulerAngles=新向量2(0,180);
}
如果(Input.GetButtonDown(“垂直”)&&IsGround&&Is跳转){
//rigidbody2D.AddForce(transform.up*力);
GetComponent().AddForce(transform.up*force);
跳跃时间=跳跃延迟;
animator.SetTrigger(“跳跃”);
跳跃=正确;
}
jumpTime-=Time.deltaTime;
如果(jumpTime我认为您应该使用EventTrigger OnPointerDown和OnPointerUp事件:
我希望它能帮助您对于跳转动作,您需要的是一个按钮。游戏对象->用户界面->按钮。用您自己的图像替换图像,然后单击“设置本机大小”。将按钮的大小重新调整为适合您游戏的大小
将按钮连接到下面的跳转按钮插槽脚本
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();
}
为什么不使用触摸输入?在这里您可以找到更多详细信息:。从UnityEngine.EventSystems实现IPInterDownHandler和IPInterUpHandler(听起来比实际情况更糟,有很多关于事件系统的教程)如果你在必要的时候不在问题中添加代码,你会被否决。很高兴你改变了这一点。下次你给我最好的答案时,请再次回复=]
#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