2D基本机芯UNITY [英] 2D basic movement UNITY

查看:88
本文介绍了2D基本机芯UNITY的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前我的角色在键盘上可以正常工作,但是当我通过3个UI按钮将您的动作转换为触摸时(我也尝试过UI图像,但成功了),我没有成功

Currently my character works perfectly on the keyboard, but when I convert your movements to touch, through 3 UI Buttons (i tried UI image too, but success) i'm not succeeding

它基本上是向右,向左和跳跃.

It basically goes to the right, left, and jumps.

如何使其遵循以下说明:

How should do to make it follow these instructions:

当用户按下定向键时,角色不会停止行走,直到用户释放按钮并按下跳转播放器跳转为止.

When the user presses directional, the character does not stop walking until the user user releases the button, and when you press the jump player jump.

这是我用来在键盘上移动的脚本!

This is the script I use to move through the keyboard!

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 =]

C# if possible, remember i am using Canvas UI for these buttons =]

推荐答案

对于Jump动作,您需要的是Button. GameObject-> UI->按钮.用您自己的图像替换该图像,然后单击设置本机大小".重新调整按钮的大小,使其适合您的游戏.

For the Jump action, what you need is a Button. GameObject->UI->Button. Replace the Image with your own image and click "Set Native Size". Re-size the button to the size that's good for your game.

将按钮附加到下面的跳转按钮插槽脚本中.

Attach the Button to the Jump Button slot script below.

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的资产,可以做到这一点.您必须先导入并对其进行一些修改才能使用.观看,以了解如何导入它.

For the Left and Right Movement buttons,you should not use Button component for them like the jump button. You have to implement Virtual JoyStick to make it look realistic. Unity have Assets called CrossPlatformInputManager that can do that. You have to import it and modify it a little bit in order to use it. Watch this to understand how to import it.

现在,您可以将Input.GetAxisInput.GetAxisRaw函数替换为CrossPlatformInputManager.GetAxis("Horizontal")CrossPlatformInputManager.GetAxisRaw("Horizontal").

Now, you can replace your Input.GetAxis and Input.GetAxisRaw functions with CrossPlatformInputManager.GetAxis("Horizontal") and CrossPlatformInputManager.GetAxisRaw("Horizontal").

如果您可以使用它,则可以在下面使用它使您的代码与移动设备和台式机兼容.

If you get it to work, then can use below to make your code comptible with both mobile and desktop.

#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

这篇关于2D基本机芯UNITY的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆