Home > front end >  Unity transform.localScale script change all the gameObject that had the script
Unity transform.localScale script change all the gameObject that had the script

Time:01-28

I have an assignment that to create a puzzle game. I have made an pretty good progress so far. Currently, I have made a script that change grabbed object size with E and Q and I assign it to every object that the player can grab.

The problem is that when I press Q and E, its changed every object in the screen including the grabbed one.

How can I fix it? Please help.

Here the code:

using UnityEngine;
using System.Collections;

[RequireComponent(typeof(MeshCollider))]

public class MouseDragAndResizing: MonoBehaviour
{

private Vector3 screenPoint;
private Vector3 offset;

void onm ouseDown()
{
    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 onm ouseDrag()
{
    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;
    }
}

}

CodePudding user response:

The problem that you don't check if your piece is selected or not, so every piece moves in update I consider to change your code to smth like this to know which piece are you actually trying to move and drag

using UnityEngine;
using System.Collections;

[RequireComponent(typeof(MeshCollider))]

public class MouseDragAndResizing: MonoBehaviour
{

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

    void onm ouseDown()
    {
        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 onm ouseUp
    {
        selected = false;
    }

    void onm ouseDrag()
    {
        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;
        }
    }
}
  •  Tags:  
  • Related