移动播放器触摸Unity C#

时间:2016-02-29 02:57:47

标签: c# touch 2d

我创建了一个简单的脚本来移动播放器的键盘输入,虽然现在我想移动播放器的触摸输入,我该怎么做?

这是我的代码,那么如何编辑此代码才能使其正常工作?我有跳跃的工作,但不知道如何移动?

using UnityEngine;
using System.Collections;

public class MoveGround : MonoBehaviour
{
    public float y = 0f;
    public Rigidbody2D rb;
    //public float x = 0f;
    //public float z = 0f;

    // Use this for initialization
    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update()
    {
        //move function

        if (Input.GetKey(KeyCode.W))
        {
            rb.velocity = new Vector2(0, y);
        }

        if (!Input.GetKey(KeyCode.W))
        {
            rb.velocity = new Vector2(0, 0);
        }

        if (Input.GetKey(KeyCode.S))
        {
            rb.velocity = new Vector2(0, -y);
        }
        //move function end
    }

    public void Move()
    {
    }
}

2 个答案:

答案 0 :(得分:0)

您希望如何使用触摸输入而不是键盘输入来移动角色?您当前的代码会移动角色,具体取决于&#34; W&#34;或&#34; S&#34;钥匙被推了。

你可以制作两个对应于&#34;转发&#34;和&#34;落后&#34;键。 将事件触发器组件添加到这些按钮。向按钮添加两种事件类型:&#34; Pointer Down&#34;和#34;指针向上。&#34; 将函数添加到每个事件的列表中。 将此代码放在场景中的对象上。

bool movingForward;
bool movingBackward;
public float speed = 0f;// set this either here in code or in the editor
public Rigidbody2D rb;

void Start (){

    rb = GetComponent<Rigidbody2D>();   

    movingForward = false;
    movingBackward = false;

}


// Your forward button will call this function
public void moveForward (){

    movingForward = true;
    movingBackward = false;

}



// Your backwardbutton will call this function
public void moveBackward (){

    movingForward = false;
    movingBackward = true;

}

//


public void stopMoving(){

    movingForward = false;
    movingBackward = false;

}    

void Update () {

    if(movingForward || movingBackward ){// we're moving

            if(movingForward ){// forward

                rb.velocity = new Vector2(0, speed);


        }else if(movingBackward ){// backward

            rb.velocity = new Vector2(0, -speed);

        }

    }else{// we're moving neither forward nor backward

        rb.velocity = new Vector2(0, 0);// so stand still

    }

}

将每个函数指向该对象。在Pointer Down事件中,选择&#34; moveForward&#34;用于向前移动角色的按钮的功能,以及&#34; moveBackward&#34;用于其他按钮的功能。 在两者的Pointer Up事件中,只需选择&#34; stopMoving&#34;功能

它非常简单,但它会起作用。

答案 1 :(得分:0)

我同意@PolakięGames。最好的方法是创建两个附加EventTrigger的UI按钮,并将方法MoveForwardMoveBackwards分配给PointerDownStopMoving到{{1 }}。虽然我会以不同的方式实现它:

PointerDown

这样你的物体就会平滑移动,你可以控制速度和加速度。如果您打算将其用于2D物理,则必须将using UnityEngine; [RequireComponent(typeof(Rigidbody))] public class MoveExample : MonoBehaviour { public float Acceleration = 4f; public float Speed = 4f; private Vector2 _velocity = Vector2.zero; private Rigidbody _rigidbody; private void Start() { _rigidbody = GetComponent<Rigidbody>(); } // movement methods public void Move(float velocity) { _velocity.x = velocity * Speed; } public void MoveForward() { Move(1f); } public void MoveBackwards() { Move(-1f); } public void StopMoving() { Move(0f); } private void Update() { _rigidbody.velocity = Vector2.Lerp(_rigidbody.velocity, _velocity, Time.deltaTime * Acceleration); } } 更改为Rigidbody