x


Scaling objects in C#

I would like to be able to make an object smaller by clicking on it with the left mouse button and bigger with right mouse button. The problems with this code are: 1. it doesn't allow to scale down unless its been scaled up 2. if there are multiple objs, they all get affected once they've been clicked on. I've tried a few different ways to do it and I'm guessing it's something to do with the resize bool. Your help is much appreciated

using UnityEngine; using System.Collections;

public class Scale : MonoBehaviour { public GameObject obj;

private float targetScale;
public float maxScale = 10.0f;
public float minScale = 2.0f;

public float shrinkSpeed = 1.0f;

private bool resizing = false;

void OnMouseDown() { resizing = true; }

void Update()
{
    if (resizing)
    {
         if (Input.GetMouseButtonDown(1)) 
        {
            targetScale = maxScale;
            //Resize();

        }
         if (Input.GetMouseButtonDown(0))
        {
            targetScale = minScale;
            //Resize();

        }

        obj.transform.localScale = Vector3.Lerp(obj.transform.localScale, new Vector3(targetScale, targetScale, targetScale), Time.deltaTime*shrinkSpeed);

        Debug.Log(obj.transform.localScale);

        if (obj.transform.localScale.x == targetScale)
        {
        resizing = false;
            Debug.Log(resizing);
        }
}
}

}

more ▼

asked Sep 26, 2013 at 05:07 PM

lollie009 gravatar image

lollie009
13 1 2 3

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

OnMouseDown() only captures a left button click, so you right button click was only working if 'resizing' had already been set to true by a left button click. To solve this problem, you'll have to move to a raycast model. Below is a bit of code implementing a 3rd party raycast model. That is, the script only has to exist in one place, not on every object. But the one drawback is that if you click on a second object before the first one has completed growing/shrinking, the growing/shrinking of the first object stops. I'm not sure of your purpose or need, so consider this example code:

using UnityEngine;
using System.Collections;

public class Scale : MonoBehaviour {

private Transform tr;

    public float maxScale = 10.0f;
    public float minScale = 2.0f;
    public float shrinkSpeed = 1.0f;    

    private float targetScale;
    private Vector3 v3Scale;


    void Update()
    {
        RaycastHit hit;
        Ray ray;

        if (Input.GetMouseButtonDown (0)) {
            ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray, out hit)) {
                tr = hit.transform;
                targetScale = minScale;
                v3Scale = new Vector3(targetScale, targetScale, targetScale);
            }
        }

        if (Input.GetMouseButtonDown (1)) {
            ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray, out hit)) {
                tr = hit.transform;
                targetScale = maxScale;
                v3Scale = new Vector3(targetScale, targetScale, targetScale);
            }
        }

        if (tr != null)
           tr.localScale = Vector3.Lerp(tr.localScale, v3Scale, Time.deltaTime*shrinkSpeed);
    }
}
more ▼

answered Sep 26, 2013 at 05:40 PM

robertbu gravatar image

robertbu
88.2k 17 31 84

Hi Robert, Thanks for your quick response. In the inspector what would I set the tr as?

Basically, it's a first person puzzle game where you have a gun that resizes objects. For example make a boulder smaller to reveal an entrance to the cave. Grow a plant of wood to cross the river etc.

Sep 26, 2013 at 06:15 PM lollie009

tr should have been private and it gets set by the code.

Sep 26, 2013 at 06:31 PM robertbu

so do I set tr as the object itself?

It seems to be shrinking as soon as I press play and it shrinks all the way down

Sep 26, 2013 at 06:33 PM lollie009

Given your expanded description, you might want a script that you attach to each game object so that you can set the maxScale and minScale independently for each object. Here is a modified version of the script above that needs to be attached to each object that you want to grow or shrink:

using UnityEngine;
using System.Collections;

public class Scale : MonoBehaviour {

    public float maxScale = 10.0f;
    public float minScale = 2.0f;
    public float shrinkSpeed = 1.0f;    

    private float targetScale;
    private Vector3 v3Scale;

    void Start() {
        v3Scale = transform.localScale;    
    }

    void Update()
    {
        RaycastHit hit;
        Ray ray;

        if (Input.GetMouseButtonDown (0)) {
            ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray, out hit) && hit.transform == transform) {
                targetScale = minScale;
                v3Scale = new Vector3(targetScale, targetScale, targetScale);
            }

        }

        if (Input.GetMouseButtonDown (1)) {
            ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray, out hit) && hit.transform == transform) {
                targetScale = maxScale;
                v3Scale = new Vector3(targetScale, targetScale, targetScale);
            }
        }

        transform.localScale = Vector3.Lerp(transform.localScale, v3Scale, Time.deltaTime*shrinkSpeed);
    }
}
Sep 26, 2013 at 06:36 PM robertbu

This works beautifully.. Thank you thank you thank you =)

Sep 26, 2013 at 06:54 PM lollie009
(comments are locked)
10|3000 characters needed characters left
private Vector3 targetScale;
public float maxScale = 10.0f;
public float minScale = 2.0f;
public float shrinkSpeed = 1.0f;
void Update(){
    if (Input.GetMouseButtonDown(1))
        targetScale.Set(maxScale,maxScale,maxScale);
    if (Input.GetMouseButtonDown(0))
        targetScale.Set(minScale,minScale,minScale);
    if (transform.localScale != targetScale)
        transform.localScale = Vector3.Lerp(transform.localScale, targetScale, Time.deltaTime*shrinkSpeed);
}

Hope this helps =)

more ▼

answered Sep 26, 2013 at 05:50 PM

Entairex1 gravatar image

Entairex1
96 6 9

Thanks for this. This sort of works except at the beginning, the object starts shrinking down to nothing. If I then hit the rmb it scales to maxScale and then lmb scales down to minScale. Except it resizes even when I'm not clicking on the object

Sep 26, 2013 at 06:26 PM lollie009

Ahhh I thought you wanted it to always be either min- or maxscale

Sep 27, 2013 at 05:03 PM Entairex1
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x8907
x178
x140
x11

asked: Sep 26, 2013 at 05:07 PM

Seen: 3666 times

Last Updated: Sep 27, 2013 at 05:03 PM