将Unity脚本设置为对象

本文关键字:对象 设置 脚本 Unity | 更新日期: 2023-09-27 17:50:44

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

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;
}

}

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

将Unity脚本设置为对象

你的滑动脚本在整个屏幕上工作的原因是因为你只是检查触摸,而不是检查你的触摸是否击中了对象,为了做到这一点,你需要做一个光线投射,你的每个对象都需要有一个碰撞器

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,光线投射将告诉你哪个游戏对象应该是你的"messagettarget"