rect transform

Hi, I am trying to do a slider in the UI with the mouse, I want to move the rect transform right side when I drag the mouse right and vice-versa I tried but it didn’t work please help me thanks in advance!!

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
public class slide : MonoBehaviour {
Vector3 prevpos = Vector3.zero;
Vector3 posdelta = Vector3.zero;

public RectTransform controllerposition;
public GameObject lastposition;

float speedFactor = 0.04f;
// Use this for initialization
void Start () {
	
}

// Update is called once per frame
void Update () {
    if (Input.GetMouseButtonDown(0))
    {
        prevpos = Input.mousePosition;
    }

    if (Input.GetMouseButton(0))
    {

        posdelta = Input.mousePosition - prevpos;

        Vector3.Distance(controllerposition.transform.position, lastposition.transform.position);
        {
            controllerposition.DOAnchorPos(new Vector2(-100, 0), 0.25f);

        }

      
    }

    prevpos = Input.mousePosition;
}

}

Your naming convention is very misleading. Your naming a game object “lastPosition” and a RectTransform “controllerPosition” and comparing their “transform” positions in your code, which raises the question. Are they referencing the same object?? I’m quite sure this is not what you want to do.


float prevXPos;
 float distToMove;
 public RectTransform rectTransform;
 float speedFactor = 0.04f;
 
 void Update () {
     if (Input.GetMouseButtonDown(0))
     {
         prevXPos = Input.mousePosition.x;
     }
     if (Input.GetMouseButton(0))
     {
         distToMove = Input.mousePosition.x - prevXPos;
         rectTransform.position =new Vector2(rectTransform.position.x + distToMove, rectTransform.position.y);
       
     }
     prevXPos = Input.mousePosition.x;
 }
   
}

That’s some untested code, but that should be more along the lines of what you want to do

@highpockets Hi thank you so much for replying but I want to add the rect transform.x in 500 when I drag the mouse is there any way to do that thanks in advance.

@highpockets hi i just want to move the position of the object in the x-direction with a mouse drag here’s the example of how the movement should be when I drag the mouse

example: MadOverGames - Touch Scroll Menu - YouTube

I did the movement in button by changing the position of the object by script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;

public class managerscipt : MonoBehaviour {
public RectTransform controllerposition;

// Use this for initialization
void Start () {
    
}

// Update is called once per frame
void Update () {
	
}

public void menupage()
{
    controllerposition.DOAnchorPos(Vector2.zero, 0.25f);

}
public void aboutpage()
{
    controllerposition.DOAnchorPos(new Vector2(-1196,0) , 0.25f);

}
public void settingspage()
{
    controllerposition.DOAnchorPos(new Vector2(-2311, 0), 0.25f);
}
public void helppage()
{
    controllerposition.DOAnchorPos(new Vector2(-3428, 0), 0.25f);

}

}

but I don’t know how to do it with the mouse drag pls help thank you so much in advance