简体   繁体   English

2D基本机芯UNITY

[英]2D basic movement UNITY

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

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# if possible, remember i am using Canvas UI for these buttons =] 如果可能,请使用C#,请记住,我正在对这些按钮使用Canvas UI =]

For the Jump action, what you need is a Button. 对于“跳转”动作,您需要一个按钮。 GameObject->UI->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. 将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();
}

For the Left and Right Movement buttons,you should not use Button component for them like the jump button. 对于移动按钮,你应该使用Button组件,用于他们像跳跃键。 You have to implement Virtual JoyStick to make it look realistic. 您必须实现Virtual JoyStick才能使其看起来逼真。 Unity have Assets called CrossPlatformInputManager that can do that. Unity具有称为CrossPlatformInputManager的资产,可以做到这一点。 You have to import it and modify it a little bit in order to use it. 您必须先导入并对其进行一些修改才能使用。 Watch this to understand how to import it. 观看此内容以了解如何导入。

Now, you can replace your Input.GetAxis and Input.GetAxisRaw functions with CrossPlatformInputManager.GetAxis("Horizontal") and CrossPlatformInputManager.GetAxisRaw("Horizontal") . 现在,您可以使用CrossPlatformInputManager.GetAxis("Horizontal")CrossPlatformInputManager.GetAxisRaw("Horizontal")替换Input.GetAxisInput.GetAxisRaw函数。

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

I think you should use EventTrigger OnPointerDown and OnPointerUp events: 我认为您应该使用EventTrigger OnPointerDown和OnPointerUp事件:

http://docs.unity3d.com/ScriptReference/EventSystems.EventTrigger.html http://docs.unity3d.com/ScriptReference/EventSystems.EventTrigger.html

I hope it helps you 希望对您有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM