2D basic movement UNITY

2019-03-06 23:51发布

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# if possible, remember i am using Canvas UI for these buttons =]

2条回答
【Aperson】
2楼-- · 2019-03-07 00:37

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();
}

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.

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
查看更多
在下西门庆
3楼-- · 2019-03-07 00:54

I think you should use EventTrigger OnPointerDown and OnPointerUp events:

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

I hope it helps you

查看更多
登录 后发表回答