- Home /
Change size of an object up to a limit
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Size_Changer : MonoBehaviour {
// Update is called once per frame
void FixedUpdate () {
if ((Input.GetKey ("space")) && (NotSURE) == Vector3(20, 20, 1))
transform.localScale += new Vector3(0.1F, 0.1F, 0);
}
}
The code will let me change the size of an object but it will keep going up even when it fills the whole screen and i would like to set a max size for the object where it cant be any bigger then can anyone help me.
Answer by a161803398874 · Aug 03, 2017 at 09:28 PM
@Dalecn You will need to compare the magnitude of you local scale value to a maximun localscale value, but you cant compare vector3 just like that.... you will need to get the magnitudes of both values... in this script you set the max local scale to five times the size of the object!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Size_Changer : MonoBehaviour {
public Vector3 maxLocalScale;
float maxlocalScaleMagnitude;
void Start(){
maxLocalScale = new Vector3 (5, 5, 1);
maxlocalScaleMagnitude = maxLocalScale.magnitude;
}
void FixedUpdate () {
float actualLocalScaleMagnitude = transform.localScale.magnitude;
if ((Input.GetKey ("space")) && (actualLocalScaleMagnitude < maxlocalScaleMagnitude))
{
transform.localScale += new Vector3(0.1F, 0.1F, 0);
}
}
}
Answer by Timo326 · Aug 03, 2017 at 09:12 PM
Try to get input in the Update() function.
Then you have to decide what you want to compare. X, Y, Z or maybe the volume?
private bool shouldGrow = false;
void Update() {
if (Input.GetKeyDown ("Space")) shouldGrow = true;
else if (Input.GetKeyUp ("Space")) shouldGrow = false;
}
void FixedUpdate () {
if (shouldGrow) {
if (transform.localScale.x < 20 &&
transform.localScale.y < 20 &&
transform.localScale.z < 1) transform.localScale += new Vector3(0.1F, 0.1F, 0);
// Or if you compare with the volume (e.g. 400):
/* if (transform.localScale.x * transform.localScale.y * transform.localScale.z < 400)
transform.localScale += new Vector3(0.1F, 0.1F, 0); */
}
}
}
Your answer
Follow this Question
Related Questions
Need some help with to grap .x with a Raycasthit (c#) 1 Answer
If gameobject moves do this 1 Answer
Check distance between multiple objects 1 Answer
Why is this script I made crashing unity 3 Answers