如何在游戏中实现这样的控制:Rolly vortex?

时间:2018-07-26 09:01:06

标签: unity3d

如何实现对视频的控制?游戏:rolly漩涡
视频-https://youtu.be/sShnjs6D59M
如何实现这样的控制?我应该使用什么?请帮助我

我的代码(已编辑):

public class TouchControl : MonoBehaviour {

    float level_width = 1f;
    float speed = 0.01f;

    void Update()
    {
        if(Input.touchCount > 0)
        {
            Debug.Log("Result = " + ((Input.GetTouch(0).position.x / Screen.width) - 0.5f) * 2);
            if(((Input.GetTouch(0).position.x / Screen.width) - 0.5f) * 2 > 0) {
                Debug.Log(((Input.GetTouch(0).position.x + 0.5f) / 2) + "  result");
                MoveLeft();
            }
            if (((Input.GetTouch(0).position.x / Screen.width) - 0.5f) * 2 < 0)
            {
                Debug.Log(((Input.GetTouch(0).position.x + 0.5f) / 2) + "  result");
                MoveRight();
            }
        }
    }

    void MoveLeft()
    {
        Vector3 newPos = transform.position;
        newPos.x = (Input.GetTouch(0).position.x * level_width) * - 1;
        transform.position = Vector3.Lerp(transform.position, newPos, speed * Time.deltaTime);
    }

    void MoveRight()
    {
        Vector3 newPos = transform.position;
        newPos.x = Input.GetTouch(0).position.x * level_width;
        transform.position = Vector3.Lerp(transform.position, newPos, speed * Time.deltaTime);
    }

}

1 个答案:

答案 0 :(得分:0)

类似的东西应该可以帮助您入门:

public float level_width = 5f;
public float speed = 0.1f; // 0..1

if(Input.touchCount > 0)
{
    Vector3 newPos = transform.position;
    newPos.x =  Input.GetTouch(0).position.x * width;
    transform.position = Vector3.Lerp(transform.position, newPos, speed * Time.deltaTime)
 }

speed将调整球到达触摸位置所需的时间。

level_width有助于将屏幕坐标的输入缩放到您的世界/级别。

编辑:我简化了您的代码:

public class TouchControl: MonoBehaviour {

    float level_width = 10f;
    float speed = 0.01 f;
    float target_xpos = 0f;

    void Update() {
        if (Input.touchCount > 0) {
            target_xpos = ((Input.GetTouch(0).position.x / Screen.width) - 0.5 f) * 2 * level_width;


            MoveToFinger();
        }
    }

    void MoveToFinger() {

        Vector3 newPos = transform.position;
        newPos.x = target_xpos;
        transform.position = Vector3.Lerp(transform.position, newPos, speed * Time.deltaTime);
    }



}