如何像本游戏一样使触摸屏移动更加平稳(Sky Rusher)?

时间:2019-04-28 04:53:06

标签: c# ios unity3d mobile

这是我要复制的内容: https://drive.google.com/open?id=1K9b7h77Ukcy5FulEX3pi9pHShyhpdmVT

我正在尝试为正在与太空飞船一起工作的移动应用程序制作这样的运动矿石。我制作了一个基本脚本,当我左右滑动并旋转它时,它可以在屏幕上移动;当我停止移动手指时,它可以将它向后移动,但是即使我觉得我的想法相同,它看起来也大不相同。作为该游戏中的录音(Sky Rusher)。我对Unity非常陌生,因此感谢您提供所有帮助!我在Unity 2018.3.6和iPhone上运行了它。这是附加到一个非常简单的纸飞机模型上的,该模型放置在原点上,并在其后方直接装有相机,因为我想先降低机芯的运动速度。提前谢谢! 以下是我的代码,如果有帮助,这是指向我上传到Google Drive

的项目的链接
using UnityEngine;
using System.Collections;
using UnityEngine.iOS;

public class ObjectShifter : MonoBehaviour
{
private Vector3 position;
private float width;
private float height;
private float shiftSpeed;
private float sideRotationAmount;
private float verticalRotationAmount;
private Quaternion rotation;

void Awake()
{
    shiftSpeed = 5f;
    sideRotationAmount = 0f;
    verticalRotationAmount = 0f;
    width = (float)Screen.width / 2.0f;
    height = (float)Screen.height / 2.0f;

    // Position used for the cube.
    position = new Vector3(0.0f, 0.0f, 0.0f);
}

void Update()
{
    // Handle screen touches.
    if (Input.touchCount > 0)
    {
        Touch touch = Input.GetTouch(0);
        Vector2 currentPos = touch.position;

        if ((touch.phase == TouchPhase.Stationary) || (touch.phase == TouchPhase.Moved && touch.deltaPosition.magnitude < 1.5))
        {
            shiftBack();
        }
        else
        {
            Vector2 pos = touch.position;
            pos.x = 3 * (pos.x - width) / width;
            pos.y = 6 * (pos.y - height) / height;
            sideRotationAmount = -15 * pos.y;
            verticalRotationAmount = -5 * pos.x;
            position = new Vector3(pos.x, pos.y, 0.0f);
            transform.position = position;
        }
    }
    else
    {
        shiftBack();
    }
    rotation = Quaternion.Euler(sideRotationAmount, verticalRotationAmount, verticalRotationAmount);
    transform.rotation = rotation;
}

void shiftBack()
{
    if(verticalRotationAmount > 0)
    {
        verticalRotationAmount -= (verticalRotationAmount%shiftSpeed);
        verticalRotationAmount-=shiftSpeed;
    }
    else if(verticalRotationAmount < 0)
    {
        verticalRotationAmount -= (verticalRotationAmount%shiftSpeed);
        verticalRotationAmount+=shiftSpeed;
    }
    if(sideRotationAmount > 0)
    {
        sideRotationAmount -= (sideRotationAmount%shiftSpeed);
        sideRotationAmount-=shiftSpeed;
    }
    else if(sideRotationAmount < 0)
    {
        sideRotationAmount -= (sideRotationAmount%shiftSpeed);
        sideRotationAmount+=shiftSpeed;
    }
}
}

0 个答案:

没有答案