- Home /
Scale in and out
I've use this code but it only does the scale out function :3 can somebody help me to fix this so it could contain the scale in function :3
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);
}
}
Your code works perfectly for me scaling both in and out. Note that your starting scale for the object should be somewhere between your $$anonymous$$Scale and maxScale. What is happening when you run this code?
Can't work out why that wouldn't work.
Could you not change the float values to vector 3 values as you are perfor$$anonymous$$g universal scaling.
So then just put targetScale = $$anonymous$$Scale without having to create a new vector each time.
Not sure if this will help. Oh and put an else if statement just to avoid messing up the target value.
When I touch the object it only keeps on getting bigger and bigger.
It worked perfectly for me. Test your code in a controlled environment.
Start a new scene.
Add a cube.
Scale your cube to (5.0, 5.0, 5.0).
Add your script (don't do any Inspector changes to the values).
Run the app and test
This is what I did, and everything was fine. Note if you are looking at values in the Inspector, Lerp() used this way can take a very long time to reach the specified size, though it will get close to the size very quickly.
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Cant change the depth on my map without it bugging out how do I fix this? 0 Answers
Scaling and performance? 0 Answers
How to scale an object between two values over distance 1 Answer
Android/IOS First Person Shooter 0 Answers