- Home /
How do i set a "object.max" scale ex.:: X axis ?
I'm having some problems with object scale ...
Are you asking how to set the horizontal size of an object in world units? Or would you like to scale it by a factor?
ie. Do you want to say "this object should be five units wide" or "this object should be stretched to double its original width"?
Answer by clunk47 · Dec 10, 2012 at 05:40 AM
You need to be specific. What language are you programming with? Here's an example in C#. Just attach this to any game object. Use the up and down arrows on your keyboard to control at runtime.
using UnityEngine;
using System.Collections;
public class scaleTest : MonoBehaviour
{
Vector3 scale;
float speed = 5.0f;
float max_X = 5.0f;
float min_X = -0.5f;
void Start()
{
scale = transform.localScale;
max_X = transform.localScale.x + max_X;
min_X = transform.localScale.x + min_X;
}
void Update()
{
transform.localScale = scale;
if(Input.GetKey(KeyCode.UpArrow))
scale.x+= speed * Time.smoothDeltaTime;
else if(Input.GetKey(KeyCode.DownArrow))
scale.x-= speed * Time.smoothDeltaTime;
if(scale.x >= max_X)
scale.x = max_X;
if(scale.x <= min_X)
scale.x = min_X;
}
}
This wont work... When you assign
scale = transform.localScale;
you are creating a copy of the current Vector3 scale representation. You are then modifying that in code. Also this code sample doesn't change the "max" value of the x-axis of the scale (whatever that means).
Yes, it will work. I did miss the $$anonymous$$AX part but will edit. This changes the scale on the x axis. Try it before you say it doesn't work.
I have added the $$anonymous$$ and max floats to the script, sorry I missed that last time.
$$anonymous$$y apologies, I missed the line where you reapply the scale.
No worries Sir. Besides, I'm glad you pointed out that I missed the max part :D
Your answer
Follow this Question
Related Questions
Object scale 1 Answer
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
Object's localScale 1 Answer
Picked up objects passes through objects 2 Answers