Why my Scale Script don't work ?
Hello, I will become crazy because my script don't work ! Why ? I am using resolution of 320 * 480 and i want save the same proportionality for my Sprite2D. (0.04 horizontaly and 0.05 verticaly)
using UnityEngine;
using System.Collections;
public class Scaling : MonoBehaviour {
void Update () {
float factor1 = (Screen.width/320)*0.04f;
float factor2 = (Screen.height/480)*0.05f;
transform.localScale = new Vector3(factor1, factor2, 0);
}
}
Please, help me, thanks.
Answer by jmonasterio · Dec 29, 2015 at 06:39 PM
Can't tell from the question what you're expecting, but I suspect the problem is a rounding error caused by division by an integer (you lose the floating point precision). Try adding .0f to the 320 and 480.
float factor1 = (Screen.width/320.0f)*0.04f;
float factor2 = (Screen.height/480.0f)*0.05f;
If this doesn't resolve the problem, try adding this at line 9, so you can see what numbers are being calculated. Maybe post the numbers you're seeing, and what numbers you want.
print( factor1);
print( factor2);
Hi, Hi, thanks for the help, but I discover another problem : why my using resolution is (305 * 458) whereas I select (640*960) ? I choose this resolution because it's double of my default resolution (320*480), so logicly : Scale x = 0.08 and Scale y = 0.1. (defaults scales with (320*480) : x = 0.04f and y = 0.05.! [Image1]
When i lauch the game with (305 * 458) resolution, it works : [Image2]
But if i choose another resolution , only the scale x is good, scale y don't change : [Image3]
Thanks very much for the help !alt text!
The images : i can't submit it in Unity answers ! $$anonymous$$y drive : https://drive.google.com/open?id=0Bwgt9hk4Zo8cNzBJSkdFS3RHYVU