Sarah
Sarah

Reputation: 135

How to scale objects in Unity3D1

Currently developing an augmented reality application and I need to change the size of my objects using pinch zoom. Found the code below on the net but it is not working. After adding the script, my object went missing and whenever I pinch it, it just appears and then it goes missing again. And it is very small like only a dot in the screen. What is the possible reason? Thanks!

    public static GameObject selectedObject;    
//public GameObject gameobject;
// Update is called once per frame
void Update () {

    if ( Input.touchCount == 0 )
    {
        Touch touch = Input.touches[0];
        Ray ray = Camera.main.ScreenPointToRay(touch.position);
        RaycastHit hit;

        if ( Physics.Raycast(ray, out hit, 100f ) )
        {
            selectedObject = hit.collider.gameObject;
        }
    }
    if (Input.touchCount == 2)
    {
        // Store both touches.
        Touch touchZero = Input.GetTouch(0);
        Touch touchOne = Input.GetTouch(1);

        // Find the position in the previous frame of each touch.
        Vector2 touchZeroPrevPos = touchZero.position - touchZero.deltaPosition;
        Vector2 touchOnePrevPos = touchOne.position - touchOne.deltaPosition;

        // Find the magnitude of the vector (the distance) between the touches in each frame.
        float prevTouchDeltaMag = (touchZeroPrevPos - touchOnePrevPos).magnitude;
        float touchDeltaMag = (touchZero.position - touchOne.position).magnitude;

        // Find the difference in the distances between each frame.
        float deltaMagnitudeDiff = prevTouchDeltaMag - touchDeltaMag;

        selectedObject.transform.localScale = new Vector3(deltaMagnitudeDiff , deltaMagnitudeDiff , deltaMagnitudeDiff);

    }
}

Upvotes: 0

Views: 1287

Answers (1)

Anish Goyal
Anish Goyal

Reputation: 2859

The issue with setting local scale using a delta is that you aren't changing the scale of the object in the way you believe you are. You're setting the scale to the delta in each axis, which may be a very very small number.

The reason why your object would disappear when the user was not scaling the object was because during that time, the value of deltaMagnitudeDiff was 0, so you were scaling your box by a factor of 0 in every direction (which shrinks it to a single point, its location). When a user was scaling the box, the box would only be as large as deltaMagnitudeDiff. So, moving your fingers faster would probably make the box appear larger than moving your fasters slowly. Once the user stopped scaling, deltaMagnitudeDiff would again be 0, since the position of the user's fingers were not scaling.

You should add your deltaMagnitudeDiff to the current local scale of the object.

Here is a modification of the last two lines of your Update() method, including the comment directly above the second to last line:

// Find the difference in distances between each frame.
float deltaMagnitudeDiff = prevTouchDeltaMag - touchDeltaMag;

Vector3 newScale = selectedObject.transform.localScale - new Vector3(deltaMagnitudeDiff, deltaMagnitudeDiff, deltaMagnitudeDiff);
selectedObject.transform.localScale = newScale;

Final script:

public static GameObject selectedObject;
//public GameObject gameobject;
// Update is called once per frame
void Update () {

    if ( Input.touchCount == 0 )
    {
        Touch touch = Input.touches[0];
        Ray ray = Camera.main.ScreenPointToRay(touch.position);
        RaycastHit hit;

        if ( Physics.Raycast(ray, out hit, 100f ) )
        {
            selectedObject = hit.collider.gameObject;
        }
    }
    if (Input.touchCount == 2)
    {
        // Store both touches.
        Touch touchZero = Input.GetTouch(0);
        Touch touchOne = Input.GetTouch(1);

        // Find the position in the previous frame of each touch.
        Vector2 touchZeroPrevPos = touchZero.position - touchZero.deltaPosition;
        Vector2 touchOnePrevPos = touchOne.position - touchOne.deltaPosition;

        // Find the magnitude of the vector (the distance) between the touches in each frame.
        float prevTouchDeltaMag = (touchZeroPrevPos - touchOnePrevPos).magnitude;
        float touchDeltaMag = (touchZero.position - touchOne.position).magnitude;

        // Find the difference in distances between each frame.
        float deltaMagnitudeDiff = prevTouchDeltaMag - touchDeltaMag;

        Vector3 newScale = selectedObject.transform.localScale - new Vector3(deltaMagnitudeDiff, deltaMagnitudeDiff, deltaMagnitudeDiff);
        selectedObject.transform.localScale = newScale;

    }
}

Upvotes: 1

Related Questions