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?
 
i have some code but i dont like how it works.
So show us the code and explain what you don't like about it. We can't know what's wrong with it if we can't see it.
 
using System.Collections;
using UnityEngine.Android;
using UnityEngine;
using System.Diagnostics;

public class ScaleChanger : MonoBehaviour
{
private Rigidbody rb;
private float deltaY;
public float sens;
private Vector3 scale;
private float deltaYX;


void Start()
{
rb = GetComponent<Rigidbody>();
}


void Update()
{

if (Input.touchCount > 0)
{
Touch touch = Input.GetTouch(0);
Vector3 touchPos = (touch.position * Time.fixedDeltaTime);

switch (touch.phase)
{
case TouchPhase.Began:
deltaY = touchPos.y - transform.localScale.y;
UnityEngine.Debug.Log("transform.localScale.y=" + transform.localScale.y+ " touchPos.y="+touchPos.y+" deltaY="+deltaY);
break;
case TouchPhase.Moved:
scale = new Vector3(Mathf.Clamp((deltaY - touchPos.y)+1 + transform.localScale.x,1, 5), Mathf.Clamp(touchPos.y - deltaY, 1, 4),1);
UnityEngine.Debug.Log("x= " + ((deltaY - touchPos.y) + 1 + transform.localScale.x)+"y="+ (touchPos.y - deltaY));
transform.localScale = scale ;
break;
}
}


}
}



this is the code.. its a bit unstable when i play and not really smooth.. im sure its wrong but im not able to find a solution. its not even working is i want it to work.. the cube goes from 1x1 to 5x1 to 1x5 and i want it as shown in the picture i attached in the previous post
 
What's with not using code tags?

[CODE=csharp] Post your code here [/CODE]

Please use code tags when posting to the forum.
 
C#:
using System.Collections;
using UnityEngine.Android;
using UnityEngine;
using System.Diagnostics;

public class ScaleChanger : MonoBehaviour
{
    private Rigidbody rb;
    private float deltaY;
    public float sens;
    private Vector3 scale;
    private float deltaYX;


    void Start()
    {
        rb = GetComponent<Rigidbody>();
    }


    void Update()
    {

        if (Input.touchCount > 0)
        {
            Touch touch = Input.GetTouch(0);
            Vector3 touchPos = (touch.position * Time.fixedDeltaTime);

                switch (touch.phase)
            {
                case TouchPhase.Began:
                    deltaY = touchPos.y - transform.localScale.y;
                    UnityEngine.Debug.Log("transform.localScale.y=" + transform.localScale.y+ " touchPos.y="+touchPos.y+" deltaY="+deltaY);
                    break;
                case TouchPhase.Moved:
                    scale = new Vector3(Mathf.Clamp((deltaY  - touchPos.y)+1 + transform.localScale.x,1, 5), Mathf.Clamp(touchPos.y - deltaY, 1, 4),1);
                    UnityEngine.Debug.Log("x= " + ((deltaY - touchPos.y) + 1 + transform.localScale.x)+"y="+ (touchPos.y - deltaY));
                   transform.localScale = scale ;
                    break;
            }
            }


        }
}
 
This really isn't a practical question for us. This would be more practical for you to debug. Since we don't have your game object, we can't debug it because there is nothing for the transform to connect to, and since I don't have a touch device at hand, I also can't test it. I also don't have unity on this PC.

Why are you subtracting the deltaY from the touchPos.y then increment by 1 and and adding another axis to it?
 
I feel like OP is going about computing the height and width the wrong way. In his shoes, I would compute the area of the square -- in this case 2x2 == 4. I would suggest computing the difference between the touch starting point and the current touch position. Scale this difference with half the maximum allowable range. This becomes the new height. Since the goal is to keep the area of the rectangle constant, then the area divide by height will give the desired width for the rectangle.
 
First of all thanks a lot for your responces.. I'l try to make my self more specific.. I just want to know if i there is a better, more reliable way of reading finger movment and how could i possibly connect it to the scale of the cube so it behaves the way i told you before..
 
I suggest you follow the documentation for this : Unity - Scripting API: Input.GetTouch They have pointers and some example code. Keep an eye on VS warning section as some API may be deprecated and Unity don't always update every document on their site when they change something. Following the suggestion in #7, and those docs, you should find it easy enough to work it out. You also didn't answer my questions. But hay ho, there ya go.
 
i changed the whole code thanks to your help.. i think that it works now.. take a look and tell me if i can shrink it somehow. its not necessary but i think it can get smaller haha :p

C#:
using System.Collections;
using UnityEngine.Android;
using UnityEngine;
using System.Diagnostics;

public class ScaleChanger : MonoBehaviour
{
    private Rigidbody rb;
    private float deltaY;
    public float sens;
    private Vector3 scale;
    private float distance;


    void Start()
    {
        rb = GetComponent<Rigidbody>();
    }


    void Update()
    {

        if (Input.touchCount > 0)
        {
            Touch touch = Input.GetTouch(0);
            Vector3 touchPos = (touch.position * Time.fixedDeltaTime);
            scale = new Vector3(transform.localScale.x, transform.localScale.y, 2);
            switch (touch.phase)
            {
                case TouchPhase.Began:
                    //deltaY is the starting point of your finger
                    deltaY = touchPos.y;
                    break;
                case TouchPhase.Moved:
                    //finger down
                    if (deltaY - 3 > touchPos.y)
                    {
                        scale = new Vector3(Mathf.Clamp(transform.localScale.x + (deltaY + 3 - touchPos.y) / sens, 1, 5), Mathf.Clamp(transform.localScale.y - (deltaY + 3 - touchPos.y) / sens, 1, 5), 2);
                    }
                    //finger up
                    else if (deltaY + 3 <= touchPos.y)
                    {
                        scale = new Vector3(Mathf.Clamp(transform.localScale.x - ((deltaY + 3 - touchPos.y) * -1) / sens * 3, 1, 5), Mathf.Clamp(transform.localScale.y + ((deltaY + 3 - touchPos.y) * -1) / sens * 3, 1, 5), 2);
                        UnityEngine.Debug.Log("ok 3");
                    }
                    //finger down
                    else if (deltaY - 2 > touchPos.y)
                    {
                        scale = new Vector3(Mathf.Clamp(transform.localScale.x + (deltaY + 2 - touchPos.y) / (sens * 5), 1, 5), Mathf.Clamp(transform.localScale.y - (deltaY + 2 - touchPos.y) / (sens * 5), 1, 5), 2);
                    }
                    //finger up little
                    else if (deltaY + 2 <= touchPos.y)
                    {
                        scale = new Vector3(Mathf.Clamp(transform.localScale.x - ((deltaY + 2 - touchPos.y) * -1) / (sens * 5), 1, 5), Mathf.Clamp(transform.localScale.y + ((deltaY + 2 - touchPos.y) * -1) / (sens * 5), 1, 5), 2);
                        UnityEngine.Debug.Log("ok 2");
                    }
                    //finger down very little
                    else if (deltaY - 1.5 > touchPos.y)
                    {
                        scale = new Vector3(Mathf.Clamp(transform.localScale.x + (deltaY + 1.5f - touchPos.y) / (sens * 10), 1, 5), Mathf.Clamp(transform.localScale.y - (deltaY + 1.5f - touchPos.y) / (sens * 10), 1, 5), 2);
                    }
                    //finger up very little
                    else if (deltaY + 1.5 <= touchPos.y)
                    {
                        scale = new Vector3(Mathf.Clamp(transform.localScale.x - ((deltaY + 1.5f - touchPos.y) * -1) / (sens * 10), 1, 5), Mathf.Clamp(transform.localScale.y + ((deltaY + 1.5f - touchPos.y) * -1) / (sens * 10), 1, 5), 2);
                        UnityEngine.Debug.Log("ok 1");
                    }

                    /* deltaY live movement try (not working properly)*/
                    //finger up deltaY renew
                    if (deltaY < touchPos.y - 5)
                    {
                        deltaY = touchPos.y - 3;
                    }
                    //finger down deltaY renew
                    if (deltaY > touchPos.y + 5)
                    {
                        deltaY = touchPos.y + 4;
                    }


                    transform.localScale = scale;
                    break;


            }
        }


    }
}


Thanks a lot for your help guys :D
 
C#:
scale = new Vector3(Mathf.Clamp(transform.localScale.x + (deltaY + 3 - touchPos.y) / sens, 1, 5), Mathf.Clamp(transform.localScale.y - (deltaY + 3 - touchPos.y) / sens, 1, 5), 2);
I would only suggest you create an internal class only for calculating the vectors and your clamps. It's just preference; but I prefer to separate where the logic is calculated from and where the object would be initialised with its new parameters and use return types instead. It also makes troubleshooting easier, and its considered best practice in C#. See https://forum.unity.com/threads/implement-separate-classes-for-ui-and-logic-game.490118/ and also Robert’s Rules of Coders: #11 Separate User Interface Logic From Business Logic while it's not the best post in the world, his blog raises the same point as me.

Glad you found a solution that worked.
 
Why do does these two vary for the factor multiplied with sens depending on up or down?
C#:
if (deltaY - 3 > touchPos.y)
{
    scale = new Vector3(Mathf.Clamp(transform.localScale.x + (deltaY + 3 - touchPos.y) / sens, 1, 5),
                        Mathf.Clamp(transform.localScale.y - (deltaY + 3 - touchPos.y) / sens, 1, 5),
                        2);
}
else if (deltaY + 3 <= touchPos.y)
{
    scale = new Vector3(Mathf.Clamp(transform.localScale.x - ((deltaY + 3 - touchPos.y) * -1) / sens * 3, 1, 5),
                        Mathf.Clamp(transform.localScale.y + ((deltaY + 3 - touchPos.y) * -1) / sens * 3, 1, 5),
                        2);
}
In all the other cases, sens is multiplied by the same factor (5 or 10) regardless of up or down direction?
 
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);
}
 
Why do does these two vary for the factor multiplied with sens depending on up or down?
C#:
if (deltaY - 3 > touchPos.y)
{
    scale = new Vector3(Mathf.Clamp(transform.localScale.x + (deltaY + 3 - touchPos.y) / sens, 1, 5),
                        Mathf.Clamp(transform.localScale.y - (deltaY + 3 - touchPos.y) / sens, 1, 5),
                        2);
}
else if (deltaY + 3 <= touchPos.y)
{
    scale = new Vector3(Mathf.Clamp(transform.localScale.x - ((deltaY + 3 - touchPos.y) * -1) / sens * 3, 1, 5),
                        Mathf.Clamp(transform.localScale.y + ((deltaY + 3 - touchPos.y) * -1) / sens * 3, 1, 5),
                        2);
}
In all the other cases, sens is multiplied by the same factor (5 or 10) regardless of up or down direction?
for some reason if i dont multiply it with 3, it's slower than the other direction and i dont really know why.. it seems to be working fine like that. so i guess it fine :p
 
I would only suggest you create an internal class only for calculating the vectors and your clamps. It's just preference; but I prefer to separate where the logic is calculated from and where the object would be initialised with its new parameters and use return types instead. It also makes troubleshooting easier, and its considered best practice in C#. See https://forum.unity.com/threads/implement-separate-classes-for-ui-and-logic-game.490118/ and also Robert’s Rules of Coders: #11 Separate User Interface Logic From Business Logic while it's not the best post in the world, his blog raises the same point as me.

Glad you found a solution that worked.
thanks a lot for your help.. il look it up and il try to do what you said.. il keep u updated :D
 

Latest posts

Back
Top Bottom