Unity transform.localScale 脚本更改所有具有脚本的游戏对象

Unity transform.localScale script change all the gameObject that had the script

我有一个任务是创建一个益智游戏。到目前为止,我取得了相当不错的进步。目前,我已经制作了一个脚本,可以使用 E 和 Q 更改抓取对象的大小,并将其分配给玩家可以抓取的每个对象。

问题是当我按下 Q 和 E 时,它改变了屏幕上的每个对象,包括抓取的对象。

我该如何解决?请帮忙

这里是代码:

using UnityEngine;
using System.Collections;

[RequireComponent(typeof(MeshCollider))]

public class MouseDragAndResizing: MonoBehaviour
{

private Vector3 screenPoint;
private Vector3 offset;

void OnMouseDown()
{
    screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);

    offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
}

void OnMouseDrag()
{
    Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);

    Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
    transform.position = curPosition;

}

void Update()
{ 
    if(Input.GetKeyDown(KeyCode.E) && transform.localScale.x < 3 && transform.localScale.y < 3 && transform.localScale.z < 3)
    {
        transform.localScale = transform.localScale + new Vector3(0.5f, 0.5f, 0.5f);
    }
    if (Input.GetKeyDown(KeyCode.Q) && transform.localScale.x > 0.5 && transform.localScale.y > 0.5 && transform.localScale.z > 0.5)
    {
        transform.localScale = transform.localScale + new Vector3(-0.5f, -0.5f, -0.5f);
    }
    else if (transform.localScale.x < 0.5 && transform.localScale.y < 0.5 && transform.localScale.z < 0.5)
    {
        transform.localScale = Vector3.zero;
    }
}

}

没有检查你的棋子是否被选中,所以每个棋子都在更新中移动的问题 我考虑将您的代码更改为这样的 smth 以了解您实际要移动和拖动的是哪一块

using UnityEngine;
using System.Collections;

[RequireComponent(typeof(MeshCollider))]

public class MouseDragAndResizing: MonoBehaviour
{

    private Vector3 screenPoint;
    private Vector3 offset;
    private bool selected;

    void OnMouseDown()
    {
        screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);

        offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
        selected = true;
    }

    void OnMouseUp
    {
        selected = false;
    }

    void OnMouseDrag()
    {
        Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);

        Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
        transform.position = curPosition;
    }

    void Update()
    { 
        if (!selected){
            return;
        }
        if(Input.GetKeyDown(KeyCode.E) && transform.localScale.x < 3 && transform.localScale.y < 3 && transform.localScale.z < 3)
        {
            transform.localScale = transform.localScale + new Vector3(0.5f, 0.5f, 0.5f);
        }
        if (Input.GetKeyDown(KeyCode.Q) && transform.localScale.x > 0.5 && transform.localScale.y > 0.5 && transform.localScale.z > 0.5)
        {
            transform.localScale = transform.localScale + new Vector3(-0.5f, -0.5f, -0.5f);
        }
        else if (transform.localScale.x < 0.5 && transform.localScale.y < 0.5 && transform.localScale.z < 0.5)
        {
            transform.localScale = Vector3.zero;
        }
    }
}