我正在为我的2D游戏进行滑动,下面的代码处理游戏的横向滑动。可以把它想象成一排门之间的水平滑动(类似于Android的滑动):
using UnityEngine;
using System.Collections;
public class DoorController : MonoBehaviour {
public Camera mainCamera;
private float speed;
private float iniCam;
float delta = 0;
private Vector2 iniPos = new Vector2 ();
private Vector2 midPos = new Vector2 ();
private Vector2 endPos = new Vector2 ();
private GameObject[] numberOfDoors;
private bool auth2160;
private bool auth2880;
// Use this for initialization
void Start () {
}
IEnumerator swipe0() {
while (mainCamera.transform.position.x != 0) {
mainCamera.transform.position = Vector2.MoveTowards(mainCamera.transform.position, new Vector2 (0, 0), (float)(Time.deltaTime * (1000 + Mathf.Abs(delta))));
yield return null;
}
}
IEnumerator swipe720() {
while (mainCamera.transform.position.x != 720) {
mainCamera.transform.position = Vector2.MoveTowards(mainCamera.transform.position, new Vector2 (720, 0), (float)(Time.deltaTime * (1000 + Mathf.Abs(delta))));
yield return null;
}
}
IEnumerator swipe1440() {
while (mainCamera.transform.position.x != 1440) {
mainCamera.transform.position = Vector2.MoveTowards(mainCamera.transform.position, new Vector2 (1440, 0), (float)(Time.deltaTime * (1000 + Mathf.Abs(delta))));
yield return null;
}
}
IEnumerator swipe2160() {
while (mainCamera.transform.position.x != 2160) {
mainCamera.transform.position = Vector2.MoveTowards(mainCamera.transform.position, new Vector2 (2160, 0), (float)(Time.deltaTime * (1000 + Mathf.Abs(delta))));
yield return null;
}
}
IEnumerator swipe2880() {
while (mainCamera.transform.position.x != 2880) {
mainCamera.transform.position = Vector2.MoveTowards(mainCamera.transform.position, new Vector2 (2880, 0), (float)(Time.deltaTime * (1000 + Mathf.Abs(delta))));
yield return null;
}
}
// Update is called once per frame
void Update () {
numberOfDoors = GameObject.FindGameObjectsWithTag("Porta");
if (numberOfDoors.Length == 3) {
auth2160 = false;
auth2880 = false;
} else if (numberOfDoors.Length == 4) {
auth2160 = true;
auth2880 = false;
} else if (numberOfDoors.Length == 5) {
auth2160 = true;
auth2880 = true;
}
if (Input.touchCount > 0) {
Touch touch = Input.touches[0];
if(touch.phase == TouchPhase.Began){
StopAllCoroutines();
iniCam = mainCamera.transform.position.x;
iniPos = new Vector2(touch.position.x,0);
Debug.Log("Posiçao inicial: " + iniPos);
}
else if(touch.phase == TouchPhase.Moved){
midPos = new Vector2(touch.position.x,0);
Debug.Log("Posiçao durante: " + midPos);
//StartCoroutine("swipeLeft");
mainCamera.transform.position = new Vector2(iniCam-(midPos.x-iniPos.x)*1.5F,0);
}
else if(touch.phase == TouchPhase.Ended){
endPos = new Vector2(touch.position.x,0);
delta = iniPos.x-endPos.x; //> 0 tendencia direita;
Debug.Log("Posiçao final: " + endPos);
if(mainCamera.transform.position.x < 0 ){
StartCoroutine("swipe0");
}else if(mainCamera.transform.position.x >= 0 && mainCamera.transform.position.x < 360 && delta < 0){
StartCoroutine("swipe0");
}else if(mainCamera.transform.position.x >= 0 && mainCamera.transform.position.x < 360 && delta > 0){
StartCoroutine("swipe0");
}else if(mainCamera.transform.position.x >= 360 && mainCamera.transform.position.x < 720 && delta < 0){
StartCoroutine("swipe0");
}else if(mainCamera.transform.position.x >= 360 && mainCamera.transform.position.x < 720 && delta > 0){
StartCoroutine("swipe720");
}else if(mainCamera.transform.position.x >= 720 && mainCamera.transform.position.x < 1080 && delta < 0){
StartCoroutine("swipe720");
}else if(mainCamera.transform.position.x >= 720 && mainCamera.transform.position.x < 1080 && delta > 0){
StartCoroutine("swipe720");
}else if(mainCamera.transform.position.x >= 1080 && mainCamera.transform.position.x < 1440 && delta < 0){
StartCoroutine("swipe720");
}else if(mainCamera.transform.position.x >= 1080 && mainCamera.transform.position.x < 1440 && delta > 0){
StartCoroutine("swipe1440");
}else if(mainCamera.transform.position.x >= 1440 && mainCamera.transform.position.x < 1800 && delta < 0){
StartCoroutine("swipe1440");
}else if(mainCamera.transform.position.x >= 1440 && mainCamera.transform.position.x < 1800 && delta > 0){
StartCoroutine("swipe1440");
}else if(mainCamera.transform.position.x >= 1800 && mainCamera.transform.position.x < 2160 && delta < 0){
StartCoroutine("swipe1440");
}else if(mainCamera.transform.position.x >= 1800 && mainCamera.transform.position.x < 2160 && delta > 0){
StartCoroutine("swipe2160");
}else if(mainCamera.transform.position.x >= 2160 && mainCamera.transform.position.x < 2520 && delta < 0){
StartCoroutine("swipe2160");
}else if(mainCamera.transform.position.x >= 2160 && mainCamera.transform.position.x < 2520 && delta > 0){
StartCoroutine("swipe2160");
}else if(mainCamera.transform.position.x >= 2520 && mainCamera.transform.position.x < 2880 && delta < 0){
StartCoroutine("swipe2160");
}else if(mainCamera.transform.position.x >= 2520 && mainCamera.transform.position.x < 2880 && delta > 0){
StartCoroutine("swipe2880");
}else if(mainCamera.transform.position.x > 2880){
StartCoroutine("swipe2880");
}if(mainCamera.transform.position.x >= 1440 && !auth2160){
StopAllCoroutines();
StartCoroutine("swipe1440");
}if(mainCamera.transform.position.x >= 2160 && !auth2880){
StopAllCoroutines();
StartCoroutine("swipe2160");
}
}
}
}
}
作为团结的初学者,这是我弄明白的唯一方法。你知道更好更清洁的方法吗?如果滑动不是完全滑动,则代码会推动或拉动门。
答案 0 :(得分:0)
下面的代码只是对你写的内容的快速清理。你基本上需要做的是参数化你的协程 另外,请注意我刚刚将您的条件分组在一起。
最后,快速了解&#34; div&#34; 变量。基本上,我没有使用更大(和更笨重)的数字,如1440,2160等,我只是将值除以360并使用它。除了让它更容易阅读之外,它并没有真正做任何事情。
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class BetterSwipeExample : MonoBehaviour {
public Camera mainCamera;
private float speed;
private float iniCam;
float delta = 0;
private Vector2 iniPos = new Vector2 ();
private Vector2 midPos = new Vector2 ();
private Vector2 endPos = new Vector2 ();
private GameObject[] numberOfDoors;
private bool auth2160;
private bool auth2880;
// Use this for initialization
void Start () {
}
private IEnumerator SwipeCoroutine (float finalPos) {
while (mainCamera.transform.position.x != finalPos) {
mainCamera.transform.position =
Vector2.MoveTowards(mainCamera.transform.position,
new Vector2 (finalPos, 0), (float)(Time.deltaTime * (1000 + Mathf.Abs(delta))));
yield return null;
}
}
// Update is called once per frame
void Update () {
numberOfDoors = GameObject.FindGameObjectsWithTag("Porta");
if (numberOfDoors.Length == 3) {
auth2160 = false;
auth2880 = false;
}
else if (numberOfDoors.Length == 4) {
auth2160 = true;
auth2880 = false;
}
else if (numberOfDoors.Length == 5) {
auth2160 = true;
auth2880 = true;
}
if (Input.touchCount > 0) {
Touch touch = Input.touches[0];
if(touch.phase == TouchPhase.Began){
StopAllCoroutines();
iniCam = mainCamera.transform.position.x;
iniPos = new Vector2(touch.position.x,0);
Debug.Log("Posiçao inicial: " + iniPos);
}
else if(touch.phase == TouchPhase.Moved){
midPos = new Vector2(touch.position.x,0);
Debug.Log("Posiçao durante: " + midPos);
//StartCoroutine("swipeLeft");
mainCamera.transform.position = new Vector2(iniCam-(midPos.x-iniPos.x)*1.5F,0);
}
else if(touch.phase == TouchPhase.Ended){
endPos = new Vector2(touch.position.x,0);
delta = iniPos.x-endPos.x; //> 0 tendencia direita;
Debug.Log("Posiçao final: " + endPos);
int div = (int)mainCamera.transform.position.x / 360;
if(div >= 6 && !auth2880){
StopAllCoroutines();
StartCoroutine(SwipeCoroutine(2160));
return;
}
if(div >= 4 && !auth2160){
StopAllCoroutines();
StartCoroutine(SwipeCoroutine(1440));
return;
}
if( div < 1 ||
(div == 1 && delta < 0) ) {
StartCoroutine(SwipeCoroutine(0));
}
else if ( (div == 1 && delta > 0) ||
(div == 2) ||
(div == 3 && delta < 0) ){
StartCoroutine(SwipeCoroutine(720));
}
else if ( (div == 3 && delta > 0) ||
(div == 4) ||
(div == 5 && delta < 0) ){
StartCoroutine(SwipeCoroutine(1440));
}
else if ( (div == 5 && delta > 0) ||
(div == 6) ||
(div == 7 && delta < 0) ){
StartCoroutine(SwipeCoroutine(2160));
}
else {
StartCoroutine(SwipeCoroutine(2880));
}
}
}
}
}