- Home /
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);
}
}
}
}
Answer by robertbu · Sep 26, 2013 at 05:40 PM
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);
}
}
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.
tr should have been private and it gets set by the code.
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
Given your expanded description, you might want a script that you attach to each game object so that you can set the maxScale and $$anonymous$$Scale 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 : $$anonymous$$onoBehaviour {
public float maxScale = 10.0f;
public float $$anonymous$$Scale = 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.Get$$anonymous$$ouseButtonDown (0)) {
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit) && hit.transform == transform) {
targetScale = $$anonymous$$Scale;
v3Scale = new Vector3(targetScale, targetScale, targetScale);
}
}
if (Input.Get$$anonymous$$ouseButtonDown (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);
}
}
This works beautifully.. Thank you thank you thank you =)
Answer by Entairex1 · Sep 26, 2013 at 05:50 PM
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 =)
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 $$anonymous$$Scale. Except it resizes even when I'm not clicking on the object
Ahhh I thought you wanted it to always be either $$anonymous$$- or maxscale
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
OnMouseDown gets both Collider and UI Button 1 Answer
Texture not showing 1 Answer