does anyone know how to scale up different game objects using mouse click and after doing the scaling, the game object should not be able to scale up again. Currently my code is able to scale up but its not what i wanted. Heres my current code:
public void ScaleForRuler()
{
transform.localScale += new Vector3 (3.0F, 0, 0.1F);
}
void OnMouseDown()
{
if (touch == false)
{
if(ruler.GetComponent<Collider>().name == "Ruler")
{
ScaleForRuler ();
touch = true;
Debug.Log (touch);
}
if(TriangleThingy.GetComponent<Collider>().name == "Triangle_Set_Square_Ruler")
{
ScaleForTriangleThingy ();
touch = true;
Debug.Log (touch);
}
if (Tsquare.GetComponent<Collider> ().name == "Tsquare")
{
if (LineR.GetComponent<Collider> ().name == "LineRight" || LineL.GetComponent<Collider> ().name == "LineLeft")
{
TsquareScaleForTB ();
touch = true;
Debug.Log (touch);
}
else if (LineB.GetComponent<Collider> ().name == "LineBottom" || LineT.GetComponent<Collider> ().name == "LineTop")
{
TsquareScaleForTB ();
touch = true;
Debug.Log (touch);
}
}
if (protractor.GetComponent<Collider> ().name == "Protractor")
{
ScaleForProtractor ();
touch = true;
Debug.Log (touch);
Debug.Log ("protractor scale");
}
}
}
Stop using OnMouseDown()
, Unity has introduced since a while some new handler interfaces like IPointerDownHandler
, IDragHandler
, etc. within the EventSystem
class, which works perfectly with mouse AND touch inputs.
It's not really clear what you want to achieve, if you want to scale every single object separately when it's clicked or just scale all together regardless which one is clicked.
I'll assume you want to be able to scale all objects separately. The steps to do that are the following:
1) Add the Physics 2D Raycaster
component to your scene camera.
2) To every single object you want to scale, add this simple script:
using UnityEngine;
using UnityEngine.EventSystems;
public class ScaleExample : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler {
private Vector2 startPosition;
private float scalingFactor = .01f;
private bool isObjectAlreadyScaled = false;
public void OnBeginDrag(PointerEventData eventData) {
startPosition = Vector2.zero;
}
public void OnDrag(PointerEventData eventData) {
if (!isObjectAlreadyScaled) {
startPosition += eventData.delta;
var scalingAmount = new Vector2(Mathf.Abs(startPosition.x), Mathf.Abs(startPosition.y));
transform.localScale = (Vector3)scalingAmount * scalingFactor;
}
}
public void OnEndDrag(PointerEventData eventData) {
isObjectAlreadyScaled = true;
}
}
The example works with 2D objects (i.e.: scales up the X and Y of the object), you can ofc change this very easily in order to accomodate different coordinates scaling.
The use of the interfaces should be clear by example: OnBeginDrag
is called once as soon as the dragging starts, OnDrag
is called repeatedly every time there's a change in the position of the pointer during the drag, and OnEndDrag
is called as soon as the drag is over (i.e.: user releases the button/finger).