unity slide to resize square

smokereaper98

Member
Joined
Nov 16, 2019
Messages
12
Programming Experience
1-3
lets say i have a square 2x2. when i slide my finger upwards i want it to get bigger in the Y axes and smaller in the x axes..and when i slide my finger down i want it to get smaller in Y and bigger in X.
helpp.jpg

i have some code but i dont like how it works. can someone help me to create something better?
 
Assuming that factor is the same for up or down, the following should shorten lines 36-68 from post #10.
First declare the following class and static list within your ScaleChanger class:
C#:
class SensitivityFactor
{
    public float Level { get; set; }
    public float Factor { get; set; }
}

static List<SensitivityFactor> SensitivityFactors = new List<SensitivityFactor>()
{
    new SensitivityFactor() { Level = 3.0f, Factor =  3.0f },
    new SensitivityFactor() { Level = 2.0f, Factor =  5.0f },
    new SensitivityFactor() { Level = 1.5f, Factor = 10.0f },
}

Then you can replace lines 36-68 with:
C#:
var realDelta = deltaY - touchPos.y;
var absDelta = Math.Abs(realDelta);
var sensitivityFactor = SensitivityFactors.FirstOrDefault(sf => sf.Level >= absDelta);
if (sensitivityFactor != null)
{
    var signDelta = Math.Abs(realDelta);
    var adjust = (realDelta + sensitivityFactor.Level) * signDelta / (sens * sensitivityFactor.Factor);
    scale = new Vector3(Mathf.Clamp(transform.localScale.x - adjust, 1, 5),
                        Mathf.Clamp(transform.localScale.y + adjust, 1, 5),
                        2);
}
il change the script and i will keep you updated :D thank you very much for your help :)
 

Latest posts

Back
Top Bottom