c# - unity hold touch水平移动

标签 c# unity3d mobile touch

我不知道如何开始,我想在按住触摸时左右移动我的角色。

就像在这个游戏中:

Example Game - Stairs from Ketchapp

我只有检测屏幕左侧或右侧空间的脚本。

public float forwardSpeed = 5f;
public float sideSpeed = 5f;

private void Update()
{
    Vector3 deltaPosition = transform.forward * forwardSpeed;
    if (Input.touchCount > 0)
    {
        Vector3 touchPosition = Input.GetTouch(0).position;
        if (touchPosition.x > Screen.width * 0.5f)
            deltaPosition += transform.right * sideSpeed;
        else
            deltaPosition -= transform.right * sideSpeed;
    }
    transform.position += deltaPosition * Time.deltaTime;
}

最佳答案

这个解决方案适合我。它用于简单的方 block 破坏游戏,用于向左或向右移动 Racket 。

void Update () {
    if (Input.touchCount > 0){
        Touch touch = Input.GetTouch(0);

        int direction = (touch.position.x > (Screen.width / 2)) ? 1 : -1;
        MovePaddle(direction);
    }

}

void MovePaddle(int direction){
    float xPos = transform.position.x + (direction * Time.deltaTime * paddleSpeed);
    playerPos = new Vector3 (Mathf.Clamp (xPos, -8f, 8f), -9.5f, 0f);
    transform.position = playerPos;
}

关于c# - unity hold touch水平移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51831670/

相关文章:

c# - 字符串资源文件命名方案及管理

c# - 统一管理时间事件的正确方法

c# - 找到旋转的差异,作为随时间统一的平均值 c#

c# - 如何检测 Unity 中发生碰撞的位置?

android - 移动安卓设备的CSS : white border above background-image doesn't want to dissapear

c# - 在列表中添加新项目时出现奇怪的速度差异(C#)

c# - 删除字符串中的空格

c# - 如何从文本文件中读取数百万行并快速插入到表中

javascript - 让 Famo.us ScrollView 滚动到末尾

JQuery Mobile 加载更多选项