【问题标题】:Moving player in Subway Surf like game using left/right swipe使用左/右滑动在 Subway Surf 中移动玩家,例如游戏
【发布时间】:2017-02-11 19:08:39
【问题描述】:

我正在 Unity 中开发类似地铁冲浪者的无尽跑步游戏。 我想移动我的播放器,平滑地向左或向右滑动。 怎么办? 这是我的代码:

using UnityEngine;
using System.Collections;

public class SwipeScript3 : MonoBehaviour {

private Touch initialTouch = new Touch();
private float distance = 0;
private bool hasSwiped = false;
//Quaternion targetx = Quaternion.Euler(0, -3f, 0);
//Quaternion targety = Quaternion.Euler(0, 3f, 0);

void FixedUpdate()
{
    foreach(Touch t in Input.touches)
    {
        if (t.phase == TouchPhase.Began)
        {
            initialTouch = t;
        }
        else if (t.phase == TouchPhase.Moved && !hasSwiped)
        {
            float deltaX = initialTouch.position.x - t.position.x;
            float deltaY = initialTouch.position.y - t.position.y;
            distance = Mathf.Sqrt((deltaX * deltaX) + (deltaY * deltaY));
            bool swipedSideways = Mathf.Abs(deltaX) > Mathf.Abs(deltaY);

            if (distance > 50f)
            {
                if (swipedSideways && deltaX > 0) //swiped left
                {

                    //transform.rotation = Quaternion.Slerp (transform.rotation, targetx, Time.deltaTime * 0.8f);
                    this.transform.Rotate(new Vector3(0, -3f, 0)*Time.deltaTime);
                    //transform.position = Vector3.Lerp (transform.position,new Vector3(transform.position.x+5f,transform.position.y,transform.position.z),Time.deltaTime*2f );
                    transform.position = Vector3.Lerp (transform.position, new Vector3 (transform.position.x - 5f, transform.position.y, transform.position.z), Time.deltaTime * 5f);
                }
                else if (swipedSideways && deltaX <= 0) //swiped right
                {
                    //transform.rotation = Quaternion.Slerp (transform.rotation, targety, Time.deltaTime * 0.8f);
                    this.transform.Rotate(new Vector3(0, 3f, 0)*Time.deltaTime);
                    //transform.position = Vector3.Lerp (transform.position, new Vector3 (transform.position.x - 5f, transform.position.y, transform.position.z), Time.deltaTime * f);
                    transform.position = Vector3.Lerp (transform.position,new Vector3(transform.position.x+5f,transform.position.y,transform.position.z),Time.deltaTime*5f );
                }
                else if (!swipedSideways && deltaY > 0) //swiped down
                {
                    //this.transform.Rotate(new Vector3(0, 2f, 0));
                    transform.position = Vector3.Lerp (transform.position,new Vector3(transform.position.x,transform.position.y,transform.position.z-5f),Time.deltaTime*2f );
                }
                else if (!swipedSideways && deltaY <= 0)  //swiped up
                {
                    this.GetComponent<Rigidbody>().velocity = new Vector3(this.GetComponent<Rigidbody>().velocity.x, 0, this.GetComponent<Rigidbody>().velocity.z);
                    this.GetComponent<Rigidbody>().AddForce(new Vector3(0, 400f, 0));
                    Debug.Log ("Swiped Up");
                }

                hasSwiped = true;
            }

        }
        else if (t.phase == TouchPhase.Ended)
        {
            initialTouch = new Touch();
            hasSwiped = false;
        }
    }
}
}

【问题讨论】:

  • 请考虑将标题更改为更具可读性的名称,例如:如何在向左或向右滑动时平滑移动玩家(地铁冲浪等无尽的跑步游戏)?。由于您的帖子已经过编辑,因此我无法提出建议(

标签: unity3d


【解决方案1】:

你可以使用Vector3.Slerp(Vector3 StartPosition, Vector3 DestinationPosition, float Number)

  • Number 介于 0 和 1 之间,它表示您的对象在 StartPositionDestinationPosition 之间的位置。

  • 假设Number = 0.0f;:您的对象将位于StartPosition

  • 如果Number = 0.5f;:您的对象将介于StartPositionDestinationPosition 之间。

  • 在执行滑动操作时,您需要将Number 的值从 0 增加到 1。

  • 增加“数字”值的速度越快,对象向目的地移动的速度就越快。

  • 您应该在滑动操作开始时设置一次StartPosition,而不是在您的Vector3.Slerp() 函数中重复设置您的transform.position

您可以在 Unity Docs 中找到示例 here

希望这会有所帮助!干杯!

【讨论】:

  • EmreE 我应用了你的方法,但仍然没有结果我的播放器远程移动不顺畅......
  • 我认为你的玩家正在传送的几个原因; - 你增加你的“数字”值太快了吗?假设您有 3 个职位;左、中、右。您想将播放器从中间移动到左侧。您将起始位置指定为中间位置(在本例中为 transform.pos),将destinationPostion 指定为左侧位置。如果你增加“数字”值的速度足够慢,你的播放器应该会顺利地走到中间到左边的位置。
  • 你能用 Vector3.Lerp 尝试同样的事情吗?让我知道结果。 @hamzamustafa
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-21
  • 1970-01-01
  • 1970-01-01
  • 2022-11-25
  • 1970-01-01
相关资源
最近更新 更多