如何基于滑动旋转对象

时间:2018-11-25 07:37:35

标签: c# unity3d

与iOS App Store上名为Sky Rusher的游戏类似,我正在编写脚本以基于滑动来向后移动对象。原始游戏中的移动可让您向任意方向滑动,并且对象沿相同方向移动。但是,该对象也因缺少更好的术语而“反弹”。例如,如果向左滑动,则对象将向左倾斜,然后向后倾斜回到其原始位置。为了给我提供最好的例子,请观看以下视频演示游戏:

Sky Rusher Gameplay

这是我目前拥有的代码(该对象在滑动时也不会来回移动,不确定那是什么,但有解决方法的想法):

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MovePlayer : MonoBehaviour
{
private Vector3 currentPos;
private Vector3 touchPos;
private float screenWidth;
private float screenHeight;
private float touchX;
private float touchY;
private float objectX;
private float objectY;

// Start is called before the first frame update
void Start()
{
    touchX = 0;
    touchY = 0;
    screenWidth = (float)Screen.width / 2.0f;
    screenHeight = (float)Screen.height / 2.0f;
    currentPos = new Vector3(0.0f, 1.0f, 0.0f);
}

// Update is called once per frame
void Update()
{
    if(Input.touchCount > 0)
    {
        Touch touch = Input.GetTouch(0);

        if(touch.phase == TouchPhase.Moved)
        {

            //touchPos = new Vector3((touch.position.x - screenWidth)/screenWidth, (touch.position.y - screenHeight)/screenHeight + 1, 0.0f);
            touchPos = new Vector3(touch.position.x, touch.position.y, 0.0f);

            touchX = (touchPos.x - screenWidth)/screenHeight - 1f;
            touchY = (touchPos.y - screenHeight)/screenHeight + 1f;

            //objectX = ((currentPos.x * screenWidth) - screenWidth)/screenWidth;
            //objectY = ((currentPos.y * screenHeight) - screenHeight)/screenHeight;
            objectX = currentPos.x;
            objectY = currentPos.y;

            objectX += (touchX - objectX) * 1.5f;
            //objectY += (touchY - objectY) * 1.5f;

            if(touchX >= 0.9f)
            {
                objectX+=0.05f;
            }
            else if(touchX <= -0.9f)
            {
                objectX-=0.05f;
            }

            currentPos = new Vector3(objectX, objectY, 0.0f);
            transform.position = currentPos;
        }
    }
}

void OnGUI()
{
    /*
    // Compute a fontSize based on the size of the screen width.
    GUI.skin.label.fontSize = (int)(Screen.width / 40.0f);

    GUI.Label(new Rect(20, 20, screenWidth, screenHeight * 0.25f),
        "Pos: x = " + (objectX.ToString("f2")) +
        ", y = " + objectY.ToString("f2"));

    GUI.Label(new Rect(20, 50, screenWidth, screenHeight * 0.25f),
    "Touch: x = " + (touchX.ToString("f2")) +
    ", y = " + (touchY.ToString("f2")));
    */
}
}

移动天体时,我需要我的物体倾斜,就像在冲天机中那样。我的游戏是使用Unity Remote 5和Unity 2018.3。在iOS设备上横向播放的。

1 个答案:

答案 0 :(得分:0)

如果我正确理解了您的意思,则可以快速实现此效果

1)每次touchX >= .9f(向右移动)沿Z轴以一定角度施加localRotation。 2)每次touchX <= -.9f(您向左移动)沿Z轴以一定的负角施加localRotation

要使它看起来平滑而不跳动,请沿几帧应用旋转,首先计算目标旋转,然后以给定速度使用RotateTowards。这是您的代码,略有修改:

public float tiltEffectAngle = 20;
public float tiltEffectSpeed = 90f;

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

        if (touch.phase == TouchPhase.Moved) {

            //touchPos = new Vector3((touch.position.x - screenWidth)/screenWidth, (touch.position.y - screenHeight)/screenHeight + 1, 0.0f);
            touchPos = new Vector3(touch.position.x, touch.position.y, 0.0f);

            touchX = (touchPos.x - screenWidth) / screenHeight - 1f;
            touchY = (touchPos.y - screenHeight) / screenHeight + 1f;

            //objectX = ((currentPos.x * screenWidth) - screenWidth)/screenWidth;
            //objectY = ((currentPos.y * screenHeight) - screenHeight)/screenHeight;
            objectX = currentPos.x;
            objectY = currentPos.y;

            objectX += (touchX - objectX) * 1.5f;
            //objectY += (touchY - objectY) * 1.5f;



            if (touchX >= 0.9f) {
                objectX += 0.05f;
                targetRotation = Quaternion.Euler(
                    transform.localEulerAngles.x,
                    transform.localEulerAngles.y,
                    tiltEffectAngle);
            } else if (touchX <= -0.9f) {
                objectX -= 0.05f;
                targetRotation = Quaternion.Euler(
                    transform.localEulerAngles.x,
                    transform.localEulerAngles.y,
                    -tiltEffectAngle);
            }

            currentPos = new Vector3(objectX, objectY, 0.0f);
            transform.position = currentPos;
        }
    }
    transform.localRotation = Quaternion.RotateTowards(transform.localRotation, targetRotation, tiltEffectSpeed * Time.deltaTime);
}