将Unity脚本设置为对象

时间:2015-05-24 12:31:12

标签: c# unity3d swipe

我有一个简单的问题:为什么我的滑动脚本在整个屏幕上滑动,而它应该只在预设的预制件上工作?为什么我的移动脚本移动对象滑动它,移动所有克隆对象?我需要一些想法。感谢。

SwipeHandler:

using UnityEngine;
using System.Collections;

public class SwipeHandler : MonoBehaviour
{
public static SwipeHandler instance;
public float minMovement = 20.0f;
public bool sendUpMessage = true;
public bool sendDownMessage = true;
public bool sendLeftMessage = true;
public bool sendRightMessage = true;
public GameObject MessageTarget = null;
private Vector2 StartPos;
private int SwipeID = -1;


void Awake(){
    instance = this;
}

void Start()
{

}

void Update ()
{
    if (MessageTarget == null)
        MessageTarget = gameObject;
    foreach (var T in Input.touches)
    {
        var P = T.position;

        if (T.phase == TouchPhase.Began && SwipeID == -1)
        {
            SwipeID = T.fingerId;
            StartPos = P;
        }
        else if (T.fingerId == SwipeID)
        {
            var delta = P - StartPos;
            if (T.phase == TouchPhase.Moved && delta.magnitude > minMovement)
            {
                SwipeID = -1;
                if (Mathf.Abs(delta.x) > Mathf.Abs(delta.y))
                {
                    if (sendRightMessage && delta.x > 0)
                        MessageTarget.SendMessage("OnSwipeRight", SendMessageOptions.DontRequireReceiver);
                    else if (sendLeftMessage && delta.x < 0)
                        MessageTarget.SendMessage("OnSwipeLeft", SendMessageOptions.DontRequireReceiver);
                }
                else
                {
                    if (sendUpMessage && delta.y > 0)
                        MessageTarget.SendMessage("OnSwipeUp", SendMessageOptions.DontRequireReceiver);
                    else if (sendDownMessage && delta.y < 0)
                        MessageTarget.SendMessage("OnSwipeDown", SendMessageOptions.DontRequireReceiver);
                }
            }
            else if (T.phase == TouchPhase.Canceled || T.phase == TouchPhase.Ended)
            {
                SwipeID = -1;
                MessageTarget.SendMessage("OnTap", SendMessageOptions.DontRequireReceiver);
            }
        }
    }
}    

}

移动:

using UnityEngine;
using System.Collections;

public class Move : MonoBehaviour {
public static Move instance;
public GameObject obj;

public float speed = 80f;
protected bool swLeft;
protected bool swRight;
// Use this for initialization


void Awake(){
    instance = this;
}

void Start(){

}

// Update is called once per frame
void Update () {
    if (obj == null)
        obj = gameObject;

    //transform.Translate(Vector3.down * Time.deltaTime, Space.World);
    //transform.Translate(0, -Time.deltaTime * speed, 0, Space.World); // Camera.main.transform bu da oluyor.


    if(swLeft == true){
        obj.transform.Translate(-Time.deltaTime * speed * 35, 0, 0, Space.World);
    }else if(swRight == true){
        obj.transform.Translate(Time.deltaTime * speed * 35, 0, 0, Space.World);
    }else{
        obj.transform.Translate(0, -Time.deltaTime * speed, 0, Space.World);
    }
    //obj.transform.Translate(0, -Time.deltaTime * speed, 0, Space.World);
    //gameObject.transform.Translate(0, -Time.deltaTime * speed, 0, Space.World);
}
void OnSwipeLeft()
{
    //transform.position -= Vector3.right*200;
    //gameObject.transform.Translate(-Vector3.right * speed * Time.deltaTime);
    swLeft = true;
    swRight = false;
}
void OnSwipeRight()
{
    //gameObject.transform.Translate(Time.deltaTime * speed, 0, 0, Space.World);

    //gameObject.transform.Translate(Vector3.right * speed * Time.deltaTime);

    swRight = true;
    swLeft = false;
    //transform.position += Vector3.right*200;
}

}

正如我所说,我将脚本设置为预制,目的是根据对象的滑动来向右或向左移动对象。但是滑动可以在屏幕上的任何位置工作,而不仅仅是在对象上。

1 个答案:

答案 0 :(得分:1)

您的滑动脚本在整个屏幕上工作的原因是因为您只是检查触摸,而不是检查您的触摸是否正在击中对象。为了做到这一点,您需要进行Ray强制转换,每个您的对象需要有一个对撞机

if (T.phase == TouchPhase.Began && SwipeID == -1)
{
    Ray ray = Camera.main.ScreenPointToRay(P);
    RaycastHit hit;
    if (Physics.Raycast(ray, out hit, 100))
    {

         if(hit.collider.gameObject == "one of your clone objects")
         {
              SwipeID = T.fingerId;
              StartPos = P;
              MessageTarget = hit.collider.gameObject;
         }
    }
}

如果您的所有对象上的SwipeHandler脚本都被复制,那么他们都会检查触摸输入并发送移动消息,因为您的脚本没有检查与某个游戏对象的碰撞。从它的外观来看,你的SwipeHandler脚本应该只是一个GameObject,而光线投射会告诉你哪个游戏对象应该是你的#34; MessageTarget&#34;